Make WordPress Core

Opened 2 years ago

Last modified 3 days ago

#55184 new defect (bug)

Custom style handle attached to a custom block style is never load even if the block is in the page.

Reported by: alexandrebuffet's profile alexandrebuffet Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.9
Component: Script Loader Keywords: has-patch
Focuses: Cc:



I have a custom block based theme running on WP 5.9 that declares custom styles for certain blocks via the register_block_style function, but when I specify a style_handle the style is never enqueue even though the block is present in the page content (and core block assets are only loaded when they are rendered).

This issue ref to ticket #54457

Step-by-step reproduction instructions

  1. Create a theme
  2. Register a custom "Display" block style for the "Heading" block via register_block_style with "my-theme-block-style-css" style_handle
  3. Register a "my-theme-block-style-css" style via wp_register_style
  4. Add "Heading" block in page content with our "Display" style
  5. Check CSS, "my-theme-block-style-css" is not loaded

The relevant code

It seems that the render_block filter callback in enqueue_block_styles_assets from script-loader.php never fire.


Attachments (1)

block-styles.png (323.4 KB) - added by alexandrebuffet 2 years ago.
Block style registration code

Download all attachments as: .zip

Change History (8)

2 years ago

Block style registration code

#1 @daymobrew
2 years ago

I encountered the same issue (I called wp_register_style in the 'init' hook).

The CSS is enqueued when a theme does not include 'wp-block-styles' support (hence my workaround to enqueue when it does support that).
Enqueuing works correctly when editing the page. The issue is only on the front end.

#2 @daymobrew
2 years ago

It is supposed to have been fixed by but it isn't.

#3 @Drivingralle
22 months ago

I can confirm the problem persist in WP v6.0.1

Code to quickly test it:

add_action( 'init', function () {
	wp_register_style( 'my-fancy-quote', get_template_directory_uri() . '/my-fancy-quote-style.css' );
			'name'         => 'fancy-quote',
			'label'        => __( 'Fancy Quote', 'textdomain' ),
			'style' => 'my-fancy-quote',
} );

The handbook says the parmeter is called "style_handle":

The core says it's called "style" in some place:
and "style_handle" in other places:

Both of them are not working.

#4 @pagelab
20 months ago

  • Keywords needs-patch added

I can also confirm the bug. AFAIK the issue is that the patch on #54457 uses the render_block() filter in the enqueue_block_styles_assets function to conditionally enqueue the registered block style, but this function runs on the enqueue_block_assets hook (see wp-includes\default-filters.php), which is too late for render_block().

#5 @poena
4 months ago

#60367 was marked as a duplicate.

This ticket was mentioned in Slack in #core by annezazu. View the logs.

12 days ago

This ticket was mentioned in PR #6628 on WordPress/wordpress-develop by @petitphp.

3 days ago

  • Keywords has-patch added; needs-patch removed

Fix missing block style loaded with style_handle.

It's possible to register a block style with a handle instead of an inline style. In that case and when wp_should_load_separate_core_block_assets() is true, the current code will rely on the render_block filter to detect if the block is used on the page and the block style need to be enqueued.

This doesn't work as expected, since the function is called in enqueue_block_assets after the render_block filter has run.

This PR add a new function hooked to wp_loaded to fix the issue.

Trac ticket:

Note: See TracTickets for help on using tickets.