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:

Description

Description

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 https://core.trac.wordpress.org/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.

script-loader.php
https://github.com/WordPress/wordpress-develop/blob/master/src/wp-includes/script-loader.php#L2463-L2477

https://core.trac.wordpress.org/changeset/52262


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)

@alexandrebuffet
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).
https://gist.github.com/damiencarbery/49cbd76c30b5116fecb7a30d0439142a#file-dcwd-custom-block-styles-php

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 https://core.trac.wordpress.org/ticket/54457 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' );
	register_block_style(
		'core/quote',
		array(
			'name'         => 'fancy-quote',
			'label'        => __( 'Fancy Quote', 'textdomain' ),
			'style' => 'my-fancy-quote',
		)
	);
} );

The handbook says the parmeter is called "style_handle":
https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/

The core says it's called "style" in some place:
https://github.com/WordPress/WordPress/blob/master/wp-includes/blocks.php#L993
and "style_handle" in other places:
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-block-styles-registry.php#L41

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
#7

  • 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: https://core.trac.wordpress.org/ticket/55184

Note: See TracTickets for help on using tickets.