wp_get_first_block()WP 6.3.0

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

Хуков нет.

Возвращает

Массив. Found block, or empty array if none found.

Использование

wp_get_first_block( $blocks, $block_name );
$blocks(массив) (обязательный)
Array of blocks.
$block_name(строка) (обязательный)
Name of the block to find.

Список изменений

С версии 6.3.0 Введена.

Код wp_get_first_block() WP 6.6.2

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();
}