-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Avoid enqueuing global styles twice when running on WordPress 5.8 #32372
Conversation
as the plugin will take care of enqueuing what's necessary.
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.
Code looks good and works as advertised 👍
Just an idea... Instead of removing if ( isset( wp_styles()->registered['global-styles'] ) ) {
wp_styles()->registered['global-styles']->extra['after'][0] = $stylesheet;
} else {
wp_register_style( 'global-styles', false, array(), true, true );
wp_add_inline_style( 'global-styles', $stylesheet );
} edit: the priority of the hook may need to be changed if we go with something like the above, just to be sure it runs after the |
@aristath that sounds exactly like what my first instinct was but didn't find how to do it. Going to give it a try, thanks for sharing! |
wp_add_inline_style( 'global-styles', $stylesheet ); | ||
wp_enqueue_style( 'global-styles' ); | ||
if ( isset( wp_styles()->registered['global-styles'] ) ) { | ||
wp_styles()->registered['global-styles']->extra['after'][0] = $stylesheet; |
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 only overwrites the first element, which represents styles that come from WordPress core, so it preservess any other styles that may have been registered after.
@aristath I've implemented your suggestion, it works nicely and we seem to have used a similar approach in other places as well. Maintaining the Going to merge after test pass, unless you have other feedback. |
Tested again and everything works as expected ❤️ |
The Gutenberg plugin embeds a stylesheet that contains the global styles of the site. WordPress 5.8 does the same. So, when the plugin runs on WordPress 5.8 we need to tell WordPress core not to enqueue its styles to prevent conflicts and avoid enqueueing them twice.
Types of changes
If there's a
global-styles
style handle, the plugin will overwrite the contents of the first style element (styles that come from WordPress core). If there's not, it'll enqueue a new stylesheet with that style handle.How to test
Test the following scenarios and verify that when visiting the front end, there's a
global-styles-inline-css
stylesheet.Alternatives considered
An alternative I've tried (see aa6e0fa) was to make the plugin use a different style handle (
global-styles-gutenberg
) and deregister core's if there was one. However, maintaining the style handle consistent allows third-party to hook into it as well.