gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Method to dequeue 'wp-block-library' in editor

Open badfeather opened this issue 4 years ago • 11 comments

What problem does this address?

I would like to write my own styles for the default Gutenberg blocks without writing overrides for default styles. I am able to do this on the front-end using:

function mytheme_block_scripts() {
	wp_dequeue_style( 'wp-block-library' );
	wp_dequeue_style( 'wp-block-library-theme' );
}
add_action( 'wp_enqueue_scripts', 'mytheme_block_scripts' );

However, the default styles are still loaded in the editor. I have tried the following:

  1. add_action( 'admin_enqueue_scripts', 'mytheme_block_scripts' );
  2. add_action( 'enqueue_block_editor_assets', 'mytheme_block_scripts' );
  3. remove_action( 'enqueue_block_editor_assets', 'enqueue_editor_block_styles_assets' );

None of these methods have worked.

What is your proposed solution?

A hook to successfully dequeue default block styles in the editor.

badfeather avatar Apr 11 '22 16:04 badfeather

This might be outdated, but I thought you'd only receive the default block styles if you have add_theme_support( 'wp-block-styles' ) defined.

Some more info in the documentation here - https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/.

talldan avatar Apr 12 '22 01:04 talldan

Because @badfeather specifically mentioned wp-block-library they might be trying to go deeper than add_theme_support( 'wp-block-styles' ). The documentation is unclear and conflates a couple of things under one vague heading ("Default block styles"):

  1. There are opinionated styles that provide "nice-to-haves", like the color bar on the edge of Quote blocks. These are opt-in with add_theme_support( 'wp-block-styles' ) on both the front-end and the editor.
  2. There are structural styles that provide "must-haves", like the ability for Columns to flex side-by-side. These are opt-out on the front-end with what was originally posted. And, might be opted-out in the editor with something like:
add_action( 'enqueue_block_assets', 'remove_it', 10 );
function remove_it() {
	wp_deregister_style( 'wp-block-library' );
	wp_register_style( 'wp-block-library', '' );
}

I'm not confident about that structural opt-out code as it's been difficult for me to find any consistent documentation about this anywhere. I'd love to know more if anyone can share up-to-date information.

ALJ avatar Apr 12 '22 03:04 ALJ

I see, thanks for adding some extra detail. Seems like there might be an opportunity to improve those docs.

Maybe @badfeather will be able to confirm whether that code works.

talldan avatar Apr 12 '22 03:04 talldan

Hi @badfeather

This might be helpful for your query trying to add following line in your functions.php file If you want to use theme.json without template editing, remove the theme support: remove_theme_support( 'wp-block-library' );

also refer below URL it will be helpful for you. https://fullsiteediting.com/lessons/how-to-remove-default-block-styles/

Thanks

theminaldiwan avatar Apr 12 '22 06:04 theminaldiwan

@ALJ A-hah! Yes, your method for opting out of structural styles did the trick. I Added the following to my theme (slightly modified):

function mytheme_block_assets() {
	wp_deregister_style( 'wp-block-library' );
	wp_register_style( 'wp-block-library', '' );
}
add_action( 'enqueue_block_assets', 'mytheme_block_assets' );

@theminaldiwan To confirm, I did try adding remove_theme_support( 'wp-block-library' );. This had no effect.

Thank you all for looking into it!

badfeather avatar Apr 12 '22 16:04 badfeather

Similar/related question: I'm also running into some opinionated styles related to blocks in the editor.css and classic.css files that get loaded in the editor. Are there any similar hooks related to these styles? I'm particularly running into issues with image alignment styles and gallery styles.

badfeather avatar Apr 14 '22 22:04 badfeather

I was trying different ways to dequeue core block css just now on a clean WP install and I noticed the same. In the editor, /wp-includes/css/dist/block-library/editor.css is being loaded and it applies a couple of styles to core/columns that I don't know how to remove.

ALJ avatar Apr 15 '22 00:04 ALJ

@badfeather the closest I've come to is to add the following alongside all the rest:

wp_deregister_style('wp-edit-blocks');
wp_register_style('wp-edit-blocks', '');

But, there might be other side-effects that I can't predict. Give it a shot and let us know how it goes.

ALJ avatar Apr 15 '22 00:04 ALJ

@theminaldiwan thank you for adding that full site editing reference earlier; it was helpful to give more options for this question. It might not be immediately clear the code snippets there are mostly applicable for Block Themes that are fully bought into "Full Site Editing" (FSE), i.e. using /templates/page.html block templates rather than "classic" PHP templates.

But, even when all those code snippets are applied (at least in [email protected]) there are still some editor styles from /block-library/editor.css I can't find reference to anywhere 🤷

Seems like there might be an opportunity to improve those docs.

@talldan I totally agree with you! I'd love to help, but I don't know what to suggest. Big-picture, maybe there's an opportunity around project-wide phrasing?

There are overlapping and overloaded names, phrases, and concepts that make it difficult to get a mental foothold to launch from. E.g. the phrase in this issue question, "default block styles", might refer to Block Styles with isDefault or maybe to Default Block Styles. Does it refer to "structural", "opinionated", or maybe a third one from editor.css?

To pick on another unrelated example: "Block Templates" might refer to annotated HTML files or to JS/PHP blockType arrays. The concept is the same, two sides of the same coin. But, to somebody looking for answers the ambiguity can derail them. Throw the search phrase "Page Template" into the mix and it gets even wilder.

It's much easier to point at problems than offer solutions 😄

ALJ avatar Apr 15 '22 03:04 ALJ

Hi all, and thanks for this, special thx to @ALJ ❤️

yankiara avatar Dec 15 '22 12:12 yankiara

From my read of the conversation here it looks like the original help request was solved, so I'm going to close it as addressed.

jasmussen avatar Aug 16 '23 11:08 jasmussen