wppaste
WordPress

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
Determines the CSS selector for the block type and property provided, returning it if available.

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, root or array path.Default: 'root'
$fallbackbooleanoptional
Whether to fall back to broader selector.Default: false

Return value

string|null
CSS selector or null if 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
6–67

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 122.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
6

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.

WhenInstructionsCalls it makes
always6–36none
!empty($target) && $value && $target !== "root" && is_string($target)30–41explode()
!empty($target) && $value && $target !== "root" && !is_string($target)33–43wp_get_block_css_selector()
!empty($target) && $target !== "root" && !is_string($target) && !$value33–44_wp_array_get()
!empty($target) && $value && $target !== "root" && is_string($target)38–48explode(), wp_get_block_css_selector()
!empty($target) && $target !== "root" && is_string($target) && !$value38–49explode(), _wp_array_get()
!empty($target) && $target !== "root" && !is_string($target) && !$value41–51_wp_array_get(), wp_get_block_css_selector()
!empty($target) && $target !== "root" && !is_string($target)41–53current(), _wp_array_get()
!empty($target) && $value && $target !== "root" && !is_string($target) && $feature_selector !== null46–57current(), _wp_array_get(), ::WP_Theme_JSON()
!empty($target) && $target !== "root" && is_string($target) && !$value46–56explode(), _wp_array_get(), wp_get_block_css_selector()
!empty($target) && $target !== "root" && is_string($target)46–58explode(), current(), _wp_array_get()
!empty($target) && $target !== "root" && !is_string($target) && !$value51–62current(), _wp_array_get(), _wp_array_get()
2 further outcomes, up to 67 instructions
!empty($target) && $value && $target !== "root" && is_string($target) && $feature_selector !== null51–62explode(), current(), _wp_array_get(), ::WP_Theme_JSON()
!empty($target) && $target !== "root" && is_string($target) && !$value56–67explode(), current(), _wp_array_get(), _wp_array_get()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1226–6716
8.51226–6716
8.41226–67167 fewer instructions than PHP 8.3
8.31296–7416
8.21296–7416
8.11296–7416
7.41296–7416

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

Used by · 6

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.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 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.