wp_get_first_block( array $blocks, string $block_name ): array

In this article

Finds the first occurrence of a specific block in an array of blocks.

Parameters

$blocksarrayrequired
Array of blocks.
$block_namestringrequired
Name of the block to find.

Return

array Found block, or empty array if none found.

Source

function wp_get_first_block( $blocks, $block_name ) {
	foreach ( $blocks as $block ) {
		if ( $block_name === $block['blockName'] ) {
			return $block;
		}
		if ( ! empty( $block['innerBlocks'] ) ) {
			$found_block = wp_get_first_block( $block['innerBlocks'], $block_name );

			if ( ! empty( $found_block ) ) {
				return $found_block;
			}
		}
	}

	return array();
}

Changelog

VersionDescription
6.3.0Introduced.

User Contributed Notes

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