WP_Theme_JSON::get_blocks_metadata(): array

In this article

Returns the metadata for each block.

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

Return

array Block metadata.

Source

 * @since 5.8.0
 * @since 5.9.0 Added `duotone` key with CSS selector.
 * @since 6.1.0 Added `features` key with block support feature level selectors.
 * @since 6.3.0 Refactored and stabilized selectors API.
 *
 * @return array Block metadata.
 */
protected static function get_blocks_metadata() {
	$registry = WP_Block_Type_Registry::get_instance();
	$blocks   = $registry->get_all_registered();

	// Is there metadata for all currently registered blocks?
	$blocks = array_diff_key( $blocks, static::$blocks_metadata );
	if ( empty( $blocks ) ) {
		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 = WP_Theme_JSON::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.
		if ( ! empty( $block_type->styles ) ) {
			$style_selectors = array();
			foreach ( $block_type->styles as $style ) {

Changelog

VersionDescription
6.3.0Refactored and stabilized selectors API.
6.1.0Added features key with block support feature level selectors.
5.9.0Added duotone key with CSS selector.
5.8.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.