Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load block support styles in the <head> for block themes. #38750

Merged
merged 8 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,21 @@ function gutenberg_render_elements_support( $block_content, $block ) {
$content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
}

// Ideally styles should be loaded in the head, but blocks may be parsed
// after that, so loading in the footer for now.
// Styles should be loaded in the head for block themes
// and classic themes by default.
//
// Only for classic themes that opt into loading separate block assets
// we should load the styles in the body.
//
// See https://core.trac.wordpress.org/ticket/53494.
$separate_assets = wp_should_load_separate_core_block_assets();
$is_classic_theme = ! wp_is_block_theme();
$action_hook_name = 'wp_enqueue_scripts';
if ( $is_classic_theme && $separate_assets ) {
oandregal marked this conversation as resolved.
Show resolved Hide resolved
$action_hook_name = 'wp_footer';
}
add_action(
'wp_footer',
$action_hook_name,
function () use ( $style ) {
echo $style;
}
oandregal marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
16 changes: 13 additions & 3 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,21 @@ function gutenberg_render_layout_support_flag( $block_content, $block ) {
1
);

// Ideally styles should be loaded in the head, but blocks may be parsed
// after that, so loading in the footer for now.
// Styles should be loaded in the head for block themes
// and classic themes by default.
//
// Only for classic themes that opt into loading separate block assets
// we should load the styles in the body.
//
// See https://core.trac.wordpress.org/ticket/53494.
$separate_assets = wp_should_load_separate_core_block_assets();
$is_classic_theme = ! wp_is_block_theme();
$action_hook_name = 'wp_enqueue_scripts';
if ( $is_classic_theme && $separate_assets ) {
$action_hook_name = 'wp_footer';
}
add_action(
'wp_footer',
$action_hook_name,
function () use ( $style ) {
echo '<style>' . $style . '</style>';
}
Expand Down