wp_get_first_block

The timeline below displays how wordpress function wp_get_first_block has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Finds the first occurrence of a specific block in an array of blocks.
 *
 * @since 6.3.0
 *
 * @param array  $blocks     Array of blocks.
 * @param string $block_name Name of the block to find.
 * @return array Found block, or empty array if none found.
 */
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();
}