[wp-trac] [WordPress Trac] #60367: Using style_handle in register_block_style call does not work

WordPress Trac noreply at wordpress.org
Mon Jan 29 11:02:19 UTC 2024


#60367: Using style_handle in register_block_style call does not work
--------------------------+-----------------------------
 Reporter:  lovor         |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Editor        |    Version:  6.4.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 When `wp_should_load_separate_core_block_assets()` is true, styles which
 are enqueued by `register_block_style`, using `style_handle` key in array
 `$style_properties` are not enqueued in front end, only in editor. This
 most notably happens in FSE themes, since load separate core block assets
 is by default true in those themes.

 == Example:

 {{{
         wp_register_style( 'core-list-dashes-style',
 get_theme_file_uri('assets/css/blocks/core-list-style-dashes.css') );
         register_block_style('core/list', [
                 'name' => 'dashes',
                 'label' => 'Dashes as bullets',
                 'style_handle' => 'core-list-dashes-style'
         ]);
 }}}

 ----

 == The reason:

 On line 2666 in `wp-includes/script-loader.php`, in such cases enqueuing
 is attempted by adding static anonymous function to `render_block` filter.
 However, this filter fires before function is added and that is the reason
 why this doesn't work!

 ----

 == Proposed solution:

 Adding another property to WP_Block_Styles_Registry which would be array,
 named e.g. `blocks_to_render`. This array would consist of block names
 (e.g. `core/list`) that are about to be rendered. In `wp-includes/class-
 wp-block.php`, in `render` function, for every block that is about to be
 rendered, its name would be added to the list, if it is not already in the
 list.
 Then, on line 2666 in `enqueue_block_styles_assets` function in `wp-
 includes/script-loader.php` block would be enqueued if it is in the
 aforementioned array.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60367>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list