Title: WP_Theme_JSON::get_blocks_metadata
Published: July 20, 2021
Last modified: August 20, 2026

---

# WP_Theme_JSON::get_blocks_metadata(): array

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_theme_json/get_blocks_metadata/?output_format=md#description)
 * [Return](https://developer.wordpress.org/reference/classes/wp_theme_json/get_blocks_metadata/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wp_theme_json/get_blocks_metadata/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_theme_json/get_blocks_metadata/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_theme_json/get_blocks_metadata/?output_format=md#changelog)

Returns the metadata for each block.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_theme_json/get_blocks_metadata/?output_format=md#description)󠁿

Example:

    ```php
    {
      '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': {}
      }
    }
    ```

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

 array Block metadata.

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

    ```php
    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 = $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;
    		}

    		// If the block has custom states defined in block.json, store their selectors.
    		if ( ! empty( $block_type->selectors['states'] ) && is_array( $block_type->selectors['states'] ) ) {
    			static::$blocks_metadata[ $block_name ]['states'] = $block_type->selectors['states'];
    		}
    	}

    	return static::$blocks_metadata;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-theme-json.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/class-wp-theme-json.php#L1764)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-theme-json.php#L1764-L1846)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_theme_json/get_blocks_metadata/?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_Block_Styles_Registry::get_instance()](https://developer.wordpress.org/reference/classes/wp_block_styles_registry/get_instance/)`wp-includes/class-wp-block-styles-registry.php` |

Utility method to retrieve the main instance of the class.

  | 
| [WP_Block_Type_Registry::get_instance()](https://developer.wordpress.org/reference/classes/wp_block_type_registry/get_instance/)`wp-includes/class-wp-block-type-registry.php` |

Utility method to retrieve the main instance of the class.

  |

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

| Version | Description | 
| [6.6.0](https://developer.wordpress.org/reference/since/6.6.0/) | Updated to include block style variations from the block styles registry. | 
| [6.3.0](https://developer.wordpress.org/reference/since/6.3.0/) | Refactored and stabilized selectors API. | 
| [6.1.0](https://developer.wordpress.org/reference/since/6.1.0/) | Added `features` key with block support feature level selectors. | 
| [5.9.0](https://developer.wordpress.org/reference/since/5.9.0/) | Added `duotone` key with CSS selector. | 
| [5.8.0](https://developer.wordpress.org/reference/since/5.8.0/) | Introduced. |

## User Contributed Notes

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