WP_Theme_JSON::get_blocks_metadata(): array
- Since
- 5.8.0, 5.9.0, 6.1.0, 6.3.0, 6.6.0
- Source
wp-includes/class-wp-theme-json.php:1147
Description
Example:
{
'core/paragraph': {
'selector': 'p',
'elements': {
'link' => 'link selector',
'etc' => 'element selector'
}
},
'core/heading': {
'selector': 'h1',
'elements': {}
},
'core/image': {
'selector': '.wp-block-image',
'duotone': 'img',
'elements': {}
}
}Compatibility
- WordPress
- since 6.6.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.
Return value
array- Block metadata.
Performance profile
How much work a call to WP_Theme_JSON::get_blocks_metadata() 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
- Light
- Scaling
- Scales with input
- Instructions
- 21–26
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
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 171.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme_JSON::get_blocks_metadata() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($blocks) | 21–22 | ::WP_Block_Type_Registry(), ->get_all_registered(), ::WP_Block_Styles_Registry(), array_diff_key() |
empty($blocks) | 25–26 | ::WP_Block_Type_Registry(), ->get_all_registered(), ::WP_Block_Styles_Registry(), array_diff_key(), ->get_all_registered() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 171 | 21–26 | 21 | |
| 8.5 | 171 | 21–26 | 21 | |
| 8.4 | 171 | 21–26 | 21 | |
| 8.3 | 171 | 21–26 | 21 | |
| 8.2 | 171 | 21–26 | 21 | |
| 8.1 | 171 | 21–26 | 21 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 172 | 21–26 | 21 |
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 · 7
- wp_get_block_css_selector()Determines the CSS selector for the block type and property provided, returning it if available.
- WP_Block_Type_Registry::get_instance()Utility method to retrieve the main instance of the class.
- WP_Block_Styles_Registry::get_instance()Utility method to retrieve the main instance of the class.
- static::get_block_style_variation_selector()
- static::get_block_selectors()
- static::get_block_element_selectors()
- static::scope_selector()
Source code
protected static function get_blocks_metadata() { $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); $style_registry = WP_Block_Styles_Registry::get_instance(); // Is there metadata for all currently registered blocks? $blocks = array_diff_key( $blocks, static::$blocks_metadata ); if ( empty( $blocks ) ) { /* * New block styles may have been registered within WP_Block_Styles_Registry. * Update block metadata for any new block style variations. */ $registered_styles = $style_registry->get_all_registered(); foreach ( static::$blocks_metadata as $block_name => $block_metadata ) { if ( ! empty( $registered_styles[ $block_name ] ) ) { $style_selectors = $block_metadata['styleVariations'] ?? array(); foreach ( $registered_styles[ $block_name ] as $block_style ) { if ( ! isset( $style_selectors[ $block_style['name'] ] ) ) { $style_selectors[ $block_style['name'] ] = static::get_block_style_variation_selector( $block_style['name'], $block_metadata['selector'] ); } } static::$blocks_metadata[ $block_name ]['styleVariations'] = $style_selectors; } } return static::$blocks_metadata; } foreach ( $blocks as $block_name => $block_type ) { $root_selector = wp_get_block_css_selector( $block_type ); static::$blocks_metadata[ $block_name ]['selector'] = $root_selector; static::$blocks_metadata[ $block_name ]['selectors'] = static::get_block_selectors( $block_type, $root_selector ); $elements = static::get_block_element_selectors( $root_selector ); if ( ! empty( $elements ) ) { static::$blocks_metadata[ $block_name ]['elements'] = $elements; } // The block may or may not have a duotone selector. $duotone_selector = wp_get_block_css_selector( $block_type, 'filter.duotone' ); // Keep backwards compatibility for support.color.__experimentalDuotone. if ( null === $duotone_selector ) { $duotone_support = isset( $block_type->supports['color']['__experimentalDuotone'] ) ? $block_type->supports['color']['__experimentalDuotone'] : null; if ( $duotone_support ) { $root_selector = wp_get_block_css_selector( $block_type ); $duotone_selector = static::scope_selector( $root_selector, $duotone_support ); } } if ( null !== $duotone_selector ) { static::$blocks_metadata[ $block_name ]['duotone'] = $duotone_selector; } // If the block has style variations, append their selectors to the block metadata. $style_selectors = array(); if ( ! empty( $block_type->styles ) ) { foreach ( $block_type->styles as $style ) { $style_selectors[ $style['name'] ] = static::get_block_style_variation_selector( $style['name'], static::$blocks_metadata[ $block_name ]['selector'] ); } } // Block style variations can be registered through the WP_Block_Styles_Registry as well as block.json. $registered_styles = $style_registry->get_registered_styles_for_block( $block_name ); foreach ( $registered_styles as $style ) { $style_selectors[ $style['name'] ] = static::get_block_style_variation_selector( $style['name'], static::$blocks_metadata[ $block_name ]['selector'] ); } if ( ! empty( $style_selectors ) ) { static::$blocks_metadata[ $block_name ]['styleVariations'] = $style_selectors; } } return static::$blocks_metadata; }Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
features key with block support feature level selectors.from the docblockduotone key with CSS selector.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 tag, from
src/wp-includes/class-wp-theme-json.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.