Unregistering block styles registered by default does not work as intended
Description
Unregistering (removing) styles registered by WP is descirbed in this chapter of block editor handbook.
However, for rounded style of core image does not work as intended: I have following:
import { unregisterBlockStyle } from '@wordpress/blocks';
import domReady from '@wordpress/dom-ready';
domReady(
function() {
unregisterBlockStyle( 'core/image', 'rounded' );
}
);
I checked in the debugger, function gets invoked, however, it is too early. If I run wp.blocks.unregisterBlockStyle( 'core/image', 'rounded' ) from the console, it gets unregistered.
Step-by-step reproduction instructions
Follow the guides above
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
This code, with a minimal delay works:
domReady(
function() {
setTimeout(() => {unregisterBlockStyle( 'core/image', 'rounded' )}, 10);
}
);
Hi @Lovor01 I cannot recreate the problem, but I suggest double-checking if your JS has the correct dependencies defined, specifically wp-edit-post. Look at this comment in a similar thread for an example.
Hi @Lovor01, give the following code a try. I am running this in my custom theme:
wp.domReady( () => {
wp.blocks.unregisterBlockStyle( 'core/image', [ 'rounded' ] );
} );
Then in my functions.php file I have:
function example_editor_styles() {
// WordPress core block styles can only be unregistered using JavaScript.
wp_enqueue_script(
'example-unregister-block-styles',
get_theme_file_uri( '/assets/js/unregister-block-styles.js' ),
array( 'wp-blocks', 'wp-dom' ),
wp_get_theme()->get( 'Version' ),
true
);
}
add_action( 'admin_init', 'example_editor_styles' );
Let me know if that works for you and I'll close out the ticket. Thanks!
Since I am unable to replicate the issue with the additional code example that I provided, I am going to close out this issue. But please let me know if you are continuing to have issues or if I can be of further assistance!
See also https://github.com/WordPress/gutenberg/issues/27607 (This is still a problem, but there's a workaround in the linked issue.)
I was able to get this working using @ndiego example, but I can't get it to unregister the "fill" button style. It works for outline. Is it possible to deregister fill? I tried 'fill' and 'default' but neither worked.
I tried this is in a quick test and it seemed to work:
wp.blocks.unregisterBlockStyle( 'core/button', [ 'fill', 'outline' ] );