wp_add_global_styles_for_blocks()
- Since
- 6.1.0, 6.7.0
- Source
wp-includes/global-styles-and-settings.php:248
Compatibility
- WordPress
- since 6.7.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Performance profile
How much work a call to wp_add_global_styles_for_blocks() 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
- Heavy
- Scaling
- Scales with input
- Instructions
- 21–49
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 142.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- transienttransient
get_transient()called directly - hookthird-party callbacks
apply_filters()one call below wp_add_global_styles_for_blocks() - cacheobject cache
wp_cache_get()one call below wp_add_global_styles_for_blocks() - optionoption read or write
get_option()one call below wp_add_global_styles_for_blocks()
Further down the call graph this can also reach serialize 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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_add_global_styles_for_blocks() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$value | 21–22 | ::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->get_styles_block_nodes(), wp_is_development_mode() |
$value | 25–26 | ::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->get_styles_block_nodes(), wp_is_development_mode(), set_transient() |
!$value | 40–45 | ::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->get_styles_block_nodes(), wp_is_development_mode(), ->get_raw_data(), wp_json_encode(), md5(), get_transient() |
!$value | 44–49 | ::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->get_styles_block_nodes(), wp_is_development_mode(), ->get_raw_data(), wp_json_encode(), md5(), get_transient(), set_transient() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 142 | 21–49 | 17 | |
| 8.5 | 142 | 21–49 | 17 | |
| 8.4 | 142 | 21–49 | 17 | 18 fewer instructions than PHP 8.3 |
| 8.3 | 160 | 21–49 | 17 | |
| 8.2 | 160 | 21–49 | 17 | |
| 8.1 | 160 | 21–49 | 17 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 161 | 21–49 | 17 |
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 · 10
- wp_is_development_mode()Checks whether the site is in the given development mode.
- wp_json_encode()Encodes a variable into JSON, with some confidence checks.
- get_transient()Retrieves the value of a transient.
- wp_should_load_block_assets_on_demand()Checks whether block styles should be loaded only on-render.
- wp_add_inline_style()Adds extra CSS styles to a registered stylesheet.
- str_starts_with()Polyfill for `str_starts_with()` function added in PHP 8.0.
- wp_get_block_name_from_theme_json_path()Gets the block name from a given theme.json path.
- set_transient()Sets/updates the value of a transient.
- WP_Theme_JSON_Resolver::get_merged_data()Returns the data merged from multiple origins.
- WP_Theme_JSON_Resolver::resolve_theme_file_uris()Resolves relative paths in theme.json styles to theme absolute paths and merges them with incoming theme JSON.
Used by · 1
- wp_enqueue_global_styles()Enqueues the global styles defined via theme.json.
Source code
function wp_add_global_styles_for_blocks() { global $wp_styles; $tree = WP_Theme_JSON_Resolver::get_merged_data(); $tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $tree ); $block_nodes = $tree->get_styles_block_nodes(); $can_use_cached = ! wp_is_development_mode( 'theme' ); $update_cache = false; if ( $can_use_cached ) { // Hash the merged WP_Theme_JSON data to bust cache on settings or styles change. $cache_hash = md5( wp_json_encode( $tree->get_raw_data() ) ); $cache_key = 'wp_styles_for_blocks'; $cached = get_transient( $cache_key ); // Reset the cached data if there is no value or if the hash has changed. if ( ! is_array( $cached ) || $cached['hash'] !== $cache_hash ) { $cached = array( 'hash' => $cache_hash, 'blocks' => array(), ); // Update the cache if the hash has changed. $update_cache = true; } } foreach ( $block_nodes as $metadata ) { if ( $can_use_cached ) { // Generate a unique cache key based on the full metadata to ensure pseudo-selectors and other variations get unique keys. $cache_node_key = md5( wp_json_encode( $metadata ) ); if ( isset( $cached['blocks'][ $cache_node_key ] ) ) { $block_css = $cached['blocks'][ $cache_node_key ]; } else { $block_css = $tree->get_styles_for_block( $metadata ); $cached['blocks'][ $cache_node_key ] = $block_css; // Update the cache if the cache contents have changed. $update_cache = true; } } else { $block_css = $tree->get_styles_for_block( $metadata ); } if ( ! wp_should_load_block_assets_on_demand() ) { wp_add_inline_style( 'global-styles', $block_css ); continue; } $stylesheet_handle = 'global-styles'; /* * When `wp_should_load_block_assets_on_demand()` is true, block styles are * enqueued for each block on the page in class WP_Block's render function. * This means there will be a handle in the styles queue for each of those blocks. * Block-specific global styles should be attached to the global-styles handle, but * only for blocks on the page, thus we check if the block's handle is in the queue * before adding the inline style. * This conditional loading only applies to core blocks. * TODO: Explore how this could be expanded to third-party blocks as well. */ if ( isset( $metadata['name'] ) ) { if ( str_starts_with( $metadata['name'], 'core/' ) ) { $block_name = str_replace( 'core/', '', $metadata['name'] ); $block_handle = 'wp-block-' . $block_name; if ( in_array( $block_handle, $wp_styles->queue, true ) ) { wp_add_inline_style( $stylesheet_handle, $block_css ); } } else { wp_add_inline_style( $stylesheet_handle, $block_css ); } } // The likes of block element styles from theme.json do not have $metadata['name'] set. if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); if ( $block_name ) {Changelog
Introduced in 6.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/global-styles-and-settings.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.