#54457 closed defect (bug) (fixed)
The function to load styles only for the block you are using is not working in register_block_style.
Reported by: | shimotomoki | Owned by: | audrasjb |
---|---|---|---|
Milestone: | 5.9 | Priority: | normal |
Severity: | normal | Version: | 5.8.2 |
Component: | Script Loader | Keywords: | has-patch has-screenshots |
Focuses: | Cc: |
Description (last modified by )
Description
The style will be loaded even if the page is not in use.
(= add_filter( 'should_load_separate_core_block_assets', '__return_true' );
)
Step-by-step reproduction instructions
- Create a sample plugin, My Plugin.
Here is the code
my-plugin.php
<?php /** * Plugin Name: My Plugin */ add_filter('should_load_separate_core_block_assets', '__return_true'); add_action('enqueue_block_assets', 'test_registering_a_block_style'); add_action('admin_enqueue_scripts', 'test_registering_a_block_style'); function test_registering_a_block_style() { wp_register_style( 'my-theme-block-styles-core-heading', plugins_url('style.css', __FILE__), ); } register_block_style( 'core/heading', array( 'name' => 'my-theme-block-styles-core-heading-add', 'label' => 'Add', 'style_handle' => 'my-theme-block-styles-core-heading', // 'inline_style' => '.wp-block-heading.is-style-heading-add , .is-style-heading-add { color: blue; }', ) );
Create style.css in the same directory.
- Activate the plugin.
- Create a new post and place a heading block.
- Create a new post and place something other than a heading block. (For example, an image block).
- The my-theme-block-styles-core-heading specified in style_handle will be loaded in both the source of the page created in 3 and 4.
Originally, I thought that my-theme-block-styles-core-heading should not be loaded for pages created with 4.
The relevant code
script-loader.php
https://github.com/WordPress/wordpress-develop/blob/master/src/wp-includes/script-loader.php#L2463-L2477
Correction code
I think this bug can be handled with this kind of code.
script-loader.php
<?php // If the site loads separate styles per-block, enqueue the stylesheet on render. if ( wp_should_load_separate_core_block_assets() ) { add_filter( 'render_block', function( $html, $block ) use ($block_name, $style_properties ) { if ( $block['blockName'] === $block_name ) { wp_enqueue_style( $style_properties['style_handle'] ); } return $html; }, 10, 2 ); } else { wp_enqueue_style( $style_properties['style_handle'] ); }
Related references
Attachments (4)
Change History (14)
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
3 years ago
#5
@
3 years ago
- Owner set to audrasjb
- Status changed from new to reviewing
Self-assigning for review
@
3 years ago
After patch: the stylesheet is not enqueued if the block style variation is not used on the post
@
3 years ago
…but it is enqueued on a post that uses the block style variation (which is the expected behavior)
#6
@
3 years ago
- Keywords has-screenshots needs-refresh added; needs-testing removed
The patch looks good to me.
It only needs a few coding standards adjustments. PR incoming.
This ticket was mentioned in PR #1964 on WordPress/wordpress-develop by audrasjb.
3 years ago
#7
- Keywords needs-refresh removed
Trac ticket: https://core.trac.wordpress.org/ticket/54457
3 years ago
#10
Committed in https://core.trac.wordpress.org/changeset/52262
Hi there, welcome to WordPress Trac!
Thanks for the report, moving to the milestone for visibility.