wp_get_block_css_selector( WP_Block_Type $block_type, string|array $target = 'root', boolean $fallback = false ): string|null
- Since
- 6.3.0
- Source
wp-includes/global-styles-and-settings.php:504
Compatibility
- WordPress
- since 6.3.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.
Parameters
$block_typeWP_Block_Type- The block's type.
$targetstring|arrayoptional- The desired selector's target,
rootor array path.Default:'root' $fallbackbooleanoptional- Whether to fall back to broader selector.Default:
false
Return value
string|null- CSS selector or
nullif no selector available.
Performance profile
How much work a call to wp_get_block_css_selector() 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
- 6–67
- Plugin surface
- None
- Called by
- 6
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 122.
Nothing here hands control to plugin code.
6 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 14 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_block_css_selector() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–36 | none |
!empty($target) && $value && $target !== "root" && is_string($target) | 30–41 | explode() |
!empty($target) && $value && $target !== "root" && !is_string($target) | 33–43 | wp_get_block_css_selector() |
!empty($target) && $target !== "root" && !is_string($target) && !$value | 33–44 | _wp_array_get() |
!empty($target) && $value && $target !== "root" && is_string($target) | 38–48 | explode(), wp_get_block_css_selector() |
!empty($target) && $target !== "root" && is_string($target) && !$value | 38–49 | explode(), _wp_array_get() |
!empty($target) && $target !== "root" && !is_string($target) && !$value | 41–51 | _wp_array_get(), wp_get_block_css_selector() |
!empty($target) && $target !== "root" && !is_string($target) | 41–53 | current(), _wp_array_get() |
!empty($target) && $value && $target !== "root" && !is_string($target) && $feature_selector !== null | 46–57 | current(), _wp_array_get(), ::WP_Theme_JSON() |
!empty($target) && $target !== "root" && is_string($target) && !$value | 46–56 | explode(), _wp_array_get(), wp_get_block_css_selector() |
!empty($target) && $target !== "root" && is_string($target) | 46–58 | explode(), current(), _wp_array_get() |
!empty($target) && $target !== "root" && !is_string($target) && !$value | 51–62 | current(), _wp_array_get(), _wp_array_get() |
2 further outcomes, up to 67 instructions
!empty($target) && $value && $target !== "root" && is_string($target) && $feature_selector !== null | 51–62 | explode(), current(), _wp_array_get(), ::WP_Theme_JSON() |
!empty($target) && $target !== "root" && is_string($target) && !$value | 56–67 | explode(), current(), _wp_array_get(), _wp_array_get() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 122 | 6–67 | 16 | |
| 8.5 | 122 | 6–67 | 16 | |
| 8.4 | 122 | 6–67 | 16 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 129 | 6–74 | 16 | |
| 8.2 | 129 | 6–74 | 16 | |
| 8.1 | 129 | 6–74 | 16 | |
| 7.4 | 129 | 6–74 | 16 |
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 · 3
- _wp_array_get()Accesses an array in depth based on a path of keys.
- wp_get_block_css_selector()Determines the CSS selector for the block type and property provided, returning it if available.
- WP_Theme_JSON::scope_selector()Function that scopes a selector with another one. This works a bit like SCSS nesting except the `&` operator isn't supported.
Used by · 6
- WP_Duotone::get_selector()Get the CSS selector for a block type.
- WP_Theme_JSON::get_block_selectors()Returns the selectors metadata for a block.
- WP_Theme_JSON::get_blocks_metadata()Returns the metadata for each block.
- _wp_add_block_level_preset_styles()Render the block level presets stylesheet.
- wp_get_block_css_selector()Determines the CSS selector for the block type and property provided, returning it if available.
- wp_render_block_states_support()Renders per-instance state styles on the frontend.
Source code
function wp_get_block_css_selector( $block_type, $target = 'root', $fallback = false ) { if ( empty( $target ) ) { return null; } $has_selectors = ! empty( $block_type->selectors ); // Root Selector. // Calculated before returning as it can be used as fallback for // feature selectors later on. $root_selector = null; if ( $has_selectors && isset( $block_type->selectors['root'] ) ) { // Use the selectors API if available. $root_selector = $block_type->selectors['root']; } elseif ( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) { // Use the old experimental selector supports property if set. $root_selector = $block_type->supports['__experimentalSelector']; } else { // If no root selector found, generate default block class selector. $block_name = str_replace( '/', '-', str_replace( 'core/', '', $block_type->name ) ); $root_selector = ".wp-block-{$block_name}"; } // Return selector if it's the root target we are looking for. if ( 'root' === $target ) { return $root_selector; } // If target is not `root` we have a feature or subfeature as the target. // If the target is a string convert to an array. if ( is_string( $target ) ) { $target = explode( '.', $target ); } // Feature Selectors ( May fallback to root selector ). if ( 1 === count( $target ) ) { $fallback_selector = $fallback ? $root_selector : null; // Prefer the selectors API if available. if ( $has_selectors ) { // Look for selector under `feature.root`. $path = array( current( $target ), 'root' ); $feature_selector = _wp_array_get( $block_type->selectors, $path, null ); if ( $feature_selector ) { return $feature_selector; } // Check if feature selector is set via shorthand. $feature_selector = _wp_array_get( $block_type->selectors, $target, null ); return is_string( $feature_selector ) ? $feature_selector : $fallback_selector; } // Try getting old experimental supports selector value. $path = array( current( $target ), '__experimentalSelector' ); $feature_selector = _wp_array_get( $block_type->supports, $path, null ); // Nothing to work with, provide fallback or null. if ( null === $feature_selector ) { return $fallback_selector; } // Scope the feature selector by the block's root selector. return WP_Theme_JSON::scope_selector( $root_selector, $feature_selector ); } // Subfeature selector // This may fallback either to parent feature or root selector. $subfeature_selector = null; // Use selectors API if available. if ( $has_selectors ) { $subfeature_selector = _wp_array_get( $block_type->selectors, $target, null ); } // Only return if we have a subfeature selector. if ( $subfeature_selector ) {Changelog
Introduced in 6.3.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.