Title: wp_get_block_css_selector
Published: August 8, 2023
Last modified: February 24, 2026

---

# wp_get_block_css_selector( WP_Block_Type $block_type, string|array $target, boolean $fallback = false ): string|null

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#wp--skip-link--target)

Determines the CSS selector for the block type and property provided, returning 
it if available.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#parameters)󠁿

 `$block_type`[WP_Block_Type](https://developer.wordpress.org/reference/classes/wp_block_type/)
required

The block’s type.

`$target`string|arrayrequired

The desired selector’s target, `root` or array path.

`$fallback`booleanoptional

Whether to fall back to broader selector.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#return)󠁿

 string|null CSS selector or `null` if no selector available.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#source)󠁿

    ```php
    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 ) {
    		return $subfeature_selector;
    	}

    	// To this point we don't have a subfeature selector. If a fallback
    	// has been requested, remove subfeature from target path and return
    	// results of a call for the parent feature's selector.
    	if ( $fallback ) {
    		return wp_get_block_css_selector( $block_type, $target[0], $fallback );
    	}

    	return null;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/global-styles-and-settings.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/global-styles-and-settings.php#L513)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/global-styles-and-settings.php#L513-L604)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_get_block_css_selector()](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/)`wp-includes/global-styles-and-settings.php` |

Determines the CSS selector for the block type and property provided, returning it if available.

  | 
| [WP_Theme_JSON::scope_selector()](https://developer.wordpress.org/reference/classes/wp_theme_json/scope_selector/)`wp-includes/class-wp-theme-json.php` |

Function that scopes a selector with another one. This works a bit like SCSS nesting except the `&amp;` operator isn’t supported.

  | 
| [_wp_array_get()](https://developer.wordpress.org/reference/functions/_wp_array_get/)`wp-includes/functions.php` |

Accesses an array in depth based on a path of keys.

  |

| Used by | Description | 
| [WP_Theme_JSON::get_block_selectors()](https://developer.wordpress.org/reference/classes/wp_theme_json/get_block_selectors/)`wp-includes/class-wp-theme-json.php` |

Returns the selectors metadata for a block.

  | 
| [wp_get_block_css_selector()](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/)`wp-includes/global-styles-and-settings.php` |

Determines the CSS selector for the block type and property provided, returning it if available.

  | 
| [WP_Duotone::get_selector()](https://developer.wordpress.org/reference/classes/wp_duotone/get_selector/)`wp-includes/class-wp-duotone.php` |

Get the CSS selector for a block type.

  | 
| [WP_Theme_JSON::get_blocks_metadata()](https://developer.wordpress.org/reference/classes/wp_theme_json/get_blocks_metadata/)`wp-includes/class-wp-theme-json.php` |

Returns the metadata for each block.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_get_block_css_selector/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.3.0](https://developer.wordpress.org/reference/since/6.3.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_block_css_selector%2F)
before being able to contribute a note or feedback.