gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Unregistering block styles registered by default does not work as intended

Open Lovor01 opened this issue 4 years ago • 7 comments

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

Lovor01 avatar Apr 09 '22 13:04 Lovor01

This code, with a minimal delay works:

domReady(
	function() {
		setTimeout(() => {unregisterBlockStyle( 'core/image', 'rounded' )}, 10);
	}
);

Lovor01 avatar Apr 09 '22 13:04 Lovor01

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.

ALJ avatar Apr 10 '22 02:04 ALJ

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!

ndiego avatar Apr 11 '22 14:04 ndiego

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!

ndiego avatar Apr 15 '22 14:04 ndiego

See also https://github.com/WordPress/gutenberg/issues/27607 (This is still a problem, but there's a workaround in the linked issue.)

markhowellsmead avatar Jan 25 '23 12:01 markhowellsmead

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.

lindsayweb avatar Jan 16 '24 18:01 lindsayweb

I tried this is in a quick test and it seemed to work:

wp.blocks.unregisterBlockStyle( 'core/button', [ 'fill', 'outline' ] );

ndiego avatar Jan 16 '24 19:01 ndiego