-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Button block: Allow for setting custom link class #14121
Comments
The referenced ticket for button is closed: There is no way to set the class to the anchor link inside the button block? |
@klihelp Depending on what exactly you need to do, you can use something like:
You can take it further from here if needed. |
Thank you @webmandesign, this is helpful for developers, but not for the content editors within the edit page. |
@klihelp You could actually tweak the code I've provided to gain such functionality you request. However, you would probably have to do some trickery, so, if we could set up a custom CSS class on button link directly in block options, it would be much better solution. But here is the simplest solution I came up with: add_filter( 'render_block', function( $block_content, $block ) {
if ( 'core/button' === $block['blockName'] && isset( $block['attrs']['className'] ) ) {
// Setting up a subset of custom button link classes.
$allowed_button_link_classes = array(
'my-custom-class',
'another-custom-class',
'example-custom-class',
// ...
);
// Remove allowed button link classes from the button container first.
$block_content = str_replace(
$allowed_button_link_classes,
'',
$block_content
);
// Get custom button classes set for the block.
$custom_classes = explode( ' ', $block['attrs']['className'] );
// Apply allowed custom button link classes.
$block_content = str_replace(
'wp-block-button__link',
'wp-block-button__link ' . implode( ' ', array_intersect( $custom_classes, $allowed_button_link_classes ) ),
$block_content
);
}
return $block_content;
}, 5, 2 ); Basically, this is looking only for a specified subset of custom button classes that can be applied on button link directly. Please set the |
Thank you, this looks really good to extend block classes as part of the theme design. |
@webmandesign This is a nice workaround, thanks for sharing. But the ability to easily filter classes on child elements for any native block should definitely be part of core. Filtering only the container class is not flexible enough. It makes something like functional CSS not feasible to use. |
Related #17194 |
Is this issue still valid? |
Well, not for me anymore as I am theme author. I can work around this issue. But I still believe this can be useful for editors, for example. |
This would be easily solved by improving the Block Variations API. Adding a |
Perhaps this issue would be something for Marcus @mkaz to take a closer look at.... |
Currently the custom block class is applied on the block container, which in Button block case is a
<div class="wp-block-button my-custom-class">
(usingmy-custom-class
as an example here).However, as most themes style some sort of button class (such as
.button
or.btn
) that could also be simply applied on links, we can not apply such class on the Button block link itself.The desired Button block output HTML would then be (using
my-custom-class
as an example here):The best would be to allow theme developers to add such class via a filter hook maybe? But surely, probably a user input field would be beneficial too.
This was also discussed in #8971
The text was updated successfully, but these errors were encountered: