Skip to content

Move wp_load_classic_theme_block_styles_on_demand() from the init action to wp_default_styles#11232

Closed
westonruter wants to merge 3 commits intoWordPress:trunkfrom
westonruter:trac-64846
Closed

Move wp_load_classic_theme_block_styles_on_demand() from the init action to wp_default_styles#11232
westonruter wants to merge 3 commits intoWordPress:trunkfrom
westonruter:trac-64846

Conversation

@westonruter
Copy link
Copy Markdown
Member

Trac ticket: https://core.trac.wordpress.org/ticket/64846

The wp_load_classic_theme_block_styles_on_demand() function was added to run at init action with priority 8 in r61008. This turns out to be too late since styles can be registered earlier. Registering a style earlier causes wp_default_styles() to be called, which registers the wp-block-library style. When a classic theme is active, before wp_load_classic_theme_block_styles_on_demand() is run, the wp_should_load_block_assets_on_demand() function will return false. This results in the combined block library stylesheet being incorrectly registered:

$path = "/wp-includes/css/dist/$package/style$suffix.css";
if ( 'block-library' === $package && wp_should_load_separate_core_block_assets() ) {
$path = "/wp-includes/css/dist/$package/common$suffix.css";
}

The point of wp_load_classic_theme_block_styles_on_demand() is to opt-in to loading separate block styles on demand, but at the point it runs it is already too late since the combined wp-block-library has already been registered.

If the init priority of wp_load_classic_theme_block_styles_on_demand() is changed to -1 then this fixes the problem, but this is not the ideal solution since a theme/plugin could always register a style earlier during the init action (e.g. PHP_INT_MIN). Note that the init action is the earliest point to register a style safely since _wp_scripts_maybe_doing_it_wrong() will issue a warning otherwise. Nevertheless, the better solution would be to not use the init action at all, but rather to use the wp_default_styles action (with a low priority) so that we'll be better guaranteed that the filters will have been added.

The issue can be reproduced with the following example plugin:

<?php
/**
 * Plugin Name: Register test style at early init.
 * Plugin URI: https://core.trac.wordpress.org/ticket/64846
 */

add_action(
	'init',
	function () {
		wp_register_style( 'test-early-init', false );
		wp_add_inline_style( 'test-early-init', '/* With a classic theme active, make sure wp-block-library is common.css and not style.css */' );
		wp_enqueue_style( 'test-early-init' );
	},
	0
);

Before ❌

<link rel='stylesheet' id='wp-block-library-css' href='http://localhost:8000/wp-includes/css/dist/block-library/style.css?ver=7.0-beta4-61919-src' media='all' />

<style id="wp-block-accordion-inline-css">
.wp-block-accordion {
  box-sizing: border-box;
}
/*# sourceURL=http://localhost:8000/wp-includes/blocks/accordion/style.css */
</style>

After ✅

<style id="wp-block-library-inline-css">
/**
 * Colors
 */
...

/*# sourceURL=/wp-includes/css/dist/block-library/common.css */
</style>
<style id="wp-block-archives-inline-css">
.wp-block-archives {
  box-sizing: border-box;
}

.wp-block-archives-dropdown label {
  display: block;
}
/*# sourceURL=http://localhost:8000/wp-includes/blocks/archives/style.css */
</style>

Use of AI Tools

None 🧠


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@westonruter westonruter requested a review from Copilot March 12, 2026 00:45
@github-actions
Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adjusts when classic themes opt into on-demand block styles (and related output-buffer enhancements) by moving the setup hook earlier in the styles bootstrapping flow, and updates the related PHPUnit test setup ordering.

Changes:

  • Move wp_load_classic_theme_block_styles_on_demand() from an init hook to wp_default_styles (priority 0).
  • Document the required call ordering for wp_load_classic_theme_block_styles_on_demand().
  • Run per-test $set_up closures earlier in test_wp_hoist_late_printed_styles().

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/phpunit/tests/template.php Runs test case setup earlier to align with the updated initialization timing.
src/wp-includes/script-loader.php Adds documentation about required ordering for classic-theme on-demand block styles initialization.
src/wp-includes/default-filters.php Changes the default hook timing for enabling classic-theme on-demand block styles.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/wp-includes/default-filters.php
Copy link
Copy Markdown
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

pento pushed a commit that referenced this pull request Mar 12, 2026
…from `init` to `wp_default_styles`.

This ensures the filters to opt in to loading separate block styles on demand are added at the moment `WP_Styles` is constructed. This accounts for styles being registered at the `init` action before `register_core_block_style_handles()` runs at priority 9. Without this, the `wp-block-library` stylesheet may get registered with the full combined block styles as `style.css` instead of just `common.css`, due to `wp_should_load_block_assets_on_demand()` still returning false. The `wp_default_styles` action still runs during `init`.

Developed in #11232

Follow-up to r61008.

Props westonruter, adamsilverstein.
See #64099.
Fixes #64846.


git-svn-id: https://develop.svn.wordpress.org/trunk@61981 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions
Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 61981
GitHub commit: f29f594

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Mar 12, 2026
markjaquith pushed a commit to WordPress/WordPress that referenced this pull request Mar 12, 2026
…from `init` to `wp_default_styles`.

This ensures the filters to opt in to loading separate block styles on demand are added at the moment `WP_Styles` is constructed. This accounts for styles being registered at the `init` action before `register_core_block_style_handles()` runs at priority 9. Without this, the `wp-block-library` stylesheet may get registered with the full combined block styles as `style.css` instead of just `common.css`, due to `wp_should_load_block_assets_on_demand()` still returning false. The `wp_default_styles` action still runs during `init`.

Developed in WordPress/wordpress-develop#11232

Follow-up to r61008.

Props westonruter, adamsilverstein.
See #64099.
Fixes #64846.

Built from https://develop.svn.wordpress.org/trunk@61981


git-svn-id: http://core.svn.wordpress.org/trunk@61263 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants