Parses blocks out of a content string.
Parameters
$contentstringrequired- Post content.
Return
array[] Array of block structures....$0arrayAn associative array of a single parsed block object. See WP_Block_Parser_Block.blockNamestringName of block.attrsarrayAttributes from block comment delimiters.innerBlocksarray[]List of inner blocks. An array of arrays that have the same structure as this one.innerHTMLstringHTML from inside block comment delimiters.innerContentarrayList of string fragments and null markers where inner blocks were found.
Source
function parse_blocks( $content ) { /** * Filter to allow plugins to replace the server-side block parser. * * @since 5.0.0 * * @param string $parser_class Name of block parser class. */ $parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' ); $parser = new $parser_class(); return $parser->parse( $content ); }Hooks
- apply_filters( ‘block_parser_class’,
string $parser_class ) Filter to allow plugins to replace the server-side block parser.
Related
Uses Description apply_filters() wp-includes/plugin.phpCalls the callback functions that have been added to a filter hook.
Show 9 moreShow lessUsed by Description resolve_pattern_blocks() wp-includes/blocks.phpReplaces patterns in a block tree with their content.
apply_block_hooks_to_content() wp-includes/blocks.phpRuns the hooked blocks algorithm on the given content.
update_ignored_hooked_blocks_postmeta() wp-includes/blocks.phpUpdates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
inject_ignored_hooked_blocks_metadata_attributes() wp-includes/block-template-utils.phpInject ignoredHookedBlocks metadata attributes into a template or template part.
wp_get_post_content_block_attributes() wp-includes/block-editor.phpRetrieves Post Content block attributes from the current post template.
WP_REST_Block_Patterns_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.phpPrepare a raw block pattern before it gets output in a REST API response.
wp_generate_block_templates_export_file() wp-includes/block-template-utils.phpCreates an export of the current templates and template parts from the site editor at the specified path in a ZIP file.
_inject_theme_attribute_in_block_template_content() wp-includes/deprecated.phpParses wp_template content and injects the active theme’s stylesheet as a theme attribute into each wp_template_part
_remove_theme_attribute_in_block_template_content() wp-includes/deprecated.phpParses a block template and removes the theme attribute from each template part.
WP_Widget_Block::get_dynamic_classname() wp-includes/widgets/class-wp-widget-block.phpCalculates the classname to use in the block widget’s container HTML.
WP_REST_Templates_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.phpPrepare a single template output for response
filter_block_content() wp-includes/blocks.phpFilters and sanitizes block content to remove non-allowable HTML from parsed block attribute values.
do_blocks() wp-includes/blocks.phpParses dynamic blocks out of
post_contentand re-renders them.excerpt_remove_blocks() wp-includes/blocks.phpParses blocks out of a content string, and renders those appropriate for the excerpt.
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_blockswill 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
blockNameis empty.$fullContent = get_the_content( $pid ); if ( has_blocks( $fullContent ) ) { doit(); }orif ( has_blocks( $post ) ) { doit(); }