Parses blocks out of a content string.
Parameters
$content
stringrequired- Post content.
Return
array[] Array of block structures....$0
arrayA representative 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
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.php
Calls the callback functions that have been added to a filter hook.
Show 11 moreShow lessUsed by Description resolve_pattern_blocks() wp-includes/blocks.php
Replaces patterns in a block tree with their content.
apply_block_hooks_to_content() wp-includes/blocks.php
Runs the hooked blocks algorithm on the given content.
update_ignored_hooked_blocks_postmeta() wp-includes/blocks.php
Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
WP_Block_Patterns_Registry::prepare_content() wp-includes/class-wp-block-patterns-registry.php
Prepares the content of a block pattern. If hooked blocks are registered, they get injected into the pattern, when they met the defined criteria.
wp_get_post_content_block_attributes() wp-includes/block-editor.php
Retrieves 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.php
Prepare a raw block pattern before it gets output in a REST API response.
wp_generate_block_templates_export_file() wp-includes/block-template-utils.php
Creates an export of the current templates and template parts from the site editor at the specified path in a ZIP file.
_build_block_template_result_from_file() wp-includes/block-template-utils.php
Builds a unified template object based on a theme file.
_build_block_template_result_from_post() wp-includes/block-template-utils.php
Builds a unified template object based a post Object.
_inject_theme_attribute_in_block_template_content() wp-includes/deprecated.php
Parses 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.php
Parses 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.php
Calculates 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.php
Prepare a single template output for response
filter_block_content() wp-includes/blocks.php
Filters and sanitizes block content to remove non-allowable HTML from parsed block attribute values.
do_blocks() wp-includes/blocks.php
Parses dynamic blocks out of
post_content
and re-renders them.excerpt_remove_blocks() wp-includes/blocks.php
Parses 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_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(); }