-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 #2323
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -1332,3 +1332,29 @@ function _wp_multiple_block_styles( $metadata ) { | |||||
return $metadata; | ||||||
} | ||||||
add_filter( 'block_type_metadata', '_wp_multiple_block_styles' ); | ||||||
|
||||||
/** | ||||||
* This function takes care of adding inline styles | ||||||
* in the proper place, depending on the theme in use. | ||||||
* | ||||||
* For block themes, it's loaded in the head. | ||||||
* For classic ones, it's loaded in the body | ||||||
* because the wp_head action (and wp_enqueue_scripts) | ||||||
* happens before the render_block. | ||||||
* | ||||||
* See https://core.trac.wordpress.org/ticket/53494. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* | ||||||
* @param string $style String containing the CSS styles to be added. | ||||||
*/ | ||||||
function wp_enqueue_block_support( $style ) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
$action_hook_name = 'wp_footer'; | ||||||
if ( wp_is_block_theme() ) { | ||||||
$action_hook_name = 'wp_enqueue_scripts'; | ||||||
} | ||||||
add_action( | ||||||
$action_hook_name, | ||||||
function () use ( $style ) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Tiny performance boost where PHP creates the embedded function once as a static instead of it being created each time the parent function is invoked. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How can someone unhook this function if desired? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This same problem exists elsewhere too. Here's the ticket for that work https://core.trac.wordpress.org/ticket/54028. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's also preexisting to this PR, note that this focused on fixing a bug with block themes (and moved this code around for convenience). I didn't have the context on why an anonymous function was used, so I didn't want to mix things. That can be updated separately. |
||||||
echo "<style>$style</style>\n"; | ||||||
} | ||||||
); | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs
@since
annotation