Make WordPress Core


Ignore:
Timestamp:
02/16/2022 05:26:46 PM (3 years ago)
Author:
jorgefilipecosta
Message:

Script Loader: Load block support styles in the head for block themes.

The dynamic block styles for layout and elements should be loaded in the head for block themes. While that should also be the case for classic themes, the current mechanism we use (render_block) does not allow us to do that, hence, this PR doesn't change anything for them and will be loaded the body.

Props oandregal, youknowriad, wpsoul.
Fixes #55148.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r52699 r52741  
    13331333}
    13341334add_filter( 'block_type_metadata', '_wp_multiple_block_styles' );
     1335
     1336/**
     1337 * This function takes care of adding inline styles
     1338 * in the proper place, depending on the theme in use.
     1339 *
     1340 * For block themes, it's loaded in the head.
     1341 * For classic ones, it's loaded in the body
     1342 * because the wp_head action (and wp_enqueue_scripts)
     1343 * happens before the render_block.
     1344 *
     1345 * See https://core.trac.wordpress.org/ticket/53494.
     1346 *
     1347 * @param string $style String containing the CSS styles to be added.
     1348 */
     1349function wp_enqueue_block_support( $style ) {
     1350    $action_hook_name = 'wp_footer';
     1351    if ( wp_is_block_theme() ) {
     1352        $action_hook_name = 'wp_enqueue_scripts';
     1353    }
     1354    add_action(
     1355        $action_hook_name,
     1356        function () use ( $style ) {
     1357            echo "<style>$style</style>\n";
     1358        }
     1359    );
     1360}
Note: See TracChangeset for help on using the changeset viewer.