FSE: Allow themes to override block styles
Is your feature request related to a problem? Please describe.
Many of the core block styling use the !important property, making it sometimes difficult for a theme author to override with their own styling.
Describe the solution you'd like Allow themes to optionally replace a core block style with its own style.
Describe alternatives you've considered
At the moment a theme can use e.g. wp_add_inline_style( 'wp-block-columns', $theme_css ) to add additional inline styling, but not completely replace the core block style.
I think it would be a good idea to let the theme decide this at the block level. Should FSE themes be allowed to remove and replace core block styles?
Have you tried registering a stylesheet using the same handle? Registering a stylesheet with the handle wp-block-columns should replace the core styles
I hadn't tried registering the stylesheet, many thanks for the suggestion. I had previously tried enqueueing the stylesheet with the core style as a dependency, but not registering the style using the core handle, so thanks for this.
Yes I can confirm this does replace the core style, although the theme registered style seems to be loaded on every post, even when said post does not contain that particular block. This is in contrast to core styles that are only loaded when required by the block being present, but I guess that is a very minor issue, if an issue at all.
Sorry my mistake, registering the stylesheet doesn't replace the core style.
I tried enqueueing the theme regd style using the wp_enqueue_scripts and wp_footer hooks, but the core Gutenberg block style is always loaded in place of the theme regd style.
Is there a another core hook/action that should be used instead?
Could you please try in wp_footer, with a priority of 1?
add_action( 'wp_footer', function() {
// Register scripts here
}, 1 );
wp_footer didn't work, but your priority suggestion gave me an idea to try init instead, which does work.
The core block style is replaced and the theme style is only rendered on posts containing the specific block.
add_action( 'init', function() {
wp_register_style( 'wp-block-columns', get_template_directory_uri() . '/assets/css/blocks/columns.css');
}, 1);
Perfect! Thank you for the update 👍 Since this was resolved and a solution was found I'll go ahead and close this ticket.