wp_load_classic_theme_block_styles_on_demand()
- Since
- 6.9.0, 7.0.0
- Source
wp-includes/script-loader.php:3690
Description
This function must be called before wp_default_styles() and register_core_block_style_handles() so that the filters are added to cause wp_should_load_separate_core_block_assets() to return true.
Compatibility
- WordPress
- since 7.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in 3 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.
Performance profile
How much work a call to wp_load_classic_theme_block_styles_on_demand() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 4–32
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 36.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()one call below wp_load_classic_theme_block_styles_on_demand()
Further down the call graph this can also reach option, cache, serialize, transient and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_load_classic_theme_block_styles_on_demand() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
wp_is_block_theme() | 4 | wp_is_block_theme() |
!wp_is_block_theme() && !wp_should_output_buffer_template_for_enhancement() | 12 | wp_is_block_theme(), add_filter(), wp_should_output_buffer_template_for_enhancement() |
!wp_is_block_theme() && wp_should_output_buffer_template_for_enhancement() && !wp_should_load_separate_core_block_assets() | 20 | wp_is_block_theme(), add_filter(), wp_should_output_buffer_template_for_enhancement(), add_filter(), wp_should_load_separate_core_block_assets() |
!wp_is_block_theme() && wp_should_output_buffer_template_for_enhancement() && wp_should_load_separate_core_block_assets() && !wp_should_load_block_assets_on_demand() | 28 | wp_is_block_theme(), add_filter(), wp_should_output_buffer_template_for_enhancement(), add_filter(), wp_should_load_separate_core_block_assets(), add_filter(), wp_should_load_block_assets_on_demand() |
!wp_is_block_theme() && wp_should_output_buffer_template_for_enhancement() && wp_should_load_separate_core_block_assets() && wp_should_load_block_assets_on_demand() | 32 | wp_is_block_theme(), add_filter(), wp_should_output_buffer_template_for_enhancement(), add_filter(), wp_should_load_separate_core_block_assets(), add_filter(), wp_should_load_block_assets_on_demand(), add_action() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 36 instructions, 4–32 executed per call, 4 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 6
- wp_is_block_theme()Returns whether the active theme is a block-based theme or not.
- add_filter()Adds a callback function to a filter hook.
- wp_should_output_buffer_template_for_enhancement()Checks whether the template should be output buffered for enhancement.
- wp_should_load_separate_core_block_assets()Checks whether separate styles should be loaded for core blocks.
- wp_should_load_block_assets_on_demand()Checks whether block styles should be loaded only on-render.
- add_action()Adds a callback function to an action hook.
Source code
function wp_load_classic_theme_block_styles_on_demand(): void { // This is not relevant to block themes, as they are opted in to loading separate styles on demand via _add_default_theme_supports(). if ( wp_is_block_theme() ) { return; } /* * Make sure that wp_should_output_buffer_template_for_enhancement() returns true even if there aren't any * `wp_template_enhancement_output_buffer` filters added, but do so at priority zero so that applications which * wish to stream responses can more easily turn this off. */ add_filter( 'wp_should_output_buffer_template_for_enhancement', '__return_true', 0 ); // If a site has opted out of the template enhancement output buffer, then bail. if ( ! wp_should_output_buffer_template_for_enhancement() ) { return; } // The following two filters are added by default for block themes in _add_default_theme_supports(). /* * Load separate block styles so that the large block-library stylesheet is not enqueued unconditionally, and so * that block-specific styles will only be enqueued when they are used on the page. A priority of zero allows for * this to be easily overridden by themes which wish to opt out. If a site has explicitly opted out of loading * separate block styles, then abort. */ add_filter( 'should_load_separate_core_block_assets', '__return_true', 0 ); if ( ! wp_should_load_separate_core_block_assets() ) { return; } /* * Also ensure that block assets are loaded on demand (although the default value is from should_load_separate_core_block_assets). * As above, a priority of zero allows for this to be easily overridden by themes which wish to opt out. If a site * has explicitly opted out of loading block styles on demand, then abort. */ add_filter( 'should_load_block_assets_on_demand', '__return_true', 0 ); if ( ! wp_should_load_block_assets_on_demand() ) { return; } // Add hooks which require the presence of the output buffer. Ideally the above two filters could be added here, but they run too early. add_action( 'wp_template_enhancement_output_buffer_started', 'wp_hoist_late_printed_styles' );}Changelog
Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.
Signature, return type and hooks compared across 3 parsed releases.
wp_default_styles action with priority 0 instead of at init with priority 8.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/script-loader.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.