Parses blocks out of a content string.
Parameters
$content
stringrequired- Post content.
Return
array[] Array of block structures....$0
arrayAn associative array of a single parsed block object. See WP_Block_Parser_Block.blockName
stringName of block.attrs
arrayAttributes from block comment delimiters.innerBlocks
array[]List of inner blocks. An array of arrays that have the same structure as this one.innerHTML
stringHTML from inside block comment delimiters.innerContent
arrayList of string fragments and null markers where inner blocks were found.
Source
return $output; } /** * Renders a single block into a HTML string. * * @since 5.0.0 * * @global WP_Post $post The post to edit. * * @param array $parsed_block { * An associative array of the block being rendered. See WP_Block_Parser_Block.
Changelog
Version Description 5.0.0 Introduced. User Contributed Notes
You must log in before being able to contribute a note or feedback.
Example using
parse_blocks()
to display a block from a postPlace within The Loop
If used in The Loop it renders the first YouTube video embedded within a post. Can be used with other blocks by assigning their blockName.
Use serialize_blocks() to convert the parsed block back to content.
Example of returned array for a Paragraph block:
As of Gutenberg 7.7, for this input:
the output of
parse_blocks
will be:In case you wish to check if current post content is Gutenberg-blocks or using Classic editor:
If post content is Classic Editor, it has only one block, but
blockName
is empty.$fullContent = get_the_content( $pid ); if ( has_blocks( $fullContent ) ) { doit(); }
orif ( has_blocks( $post ) ) { doit(); }