parse_blocks()
Parses blocks out of a content string.
Uses: WP_Block_Parser::parse()
Hooks from the function
Return
Array[]
. Array of block structures.
Usage
parse_blocks( $content );
- $content(string) (required)
- Post content.
Examples
#1 Example returned data
Let us have the following content:
<!-- wp:gallery {"ids":[11]} --> <figure class="wp-block-gallery columns-1 is-cropped"> <ul class="blocks-gallery-grid"> <li class="blocks-gallery-item"> <figure> <img src="https://example.com/wp-content/uploads/2020/05/2020-landscape-1-1024x656.png" alt="" data-id="11" data-full-url="https://example.com/wp-content/uploads/2020/05/2020-landscape-1.png" data-link="https://example.com/my-super-image/" class="wp-image-11"/> </figure> </li> </ul> </figure> <!-- /wp:gallery --> <!-- wp:cover {"overlayColor":"luminous-vivid-amber"} --> <div class="wp-block-cover has-luminous-vivid-amber-background-color has-background-dim"> <div class="wp-block-cover__inner-container"> <!-- wp:paragraph {"align": "center", "placeholder": "Enter title...", "fontSize": "large"} <p class="has-text-align-center has-large-font-size">Super block header! </p> <!-- /wp:paragraph --> </div> </div> <!-- /wp:cover --> <!-- wp:paragraph --> <p>Ordinary paragraph with text</p> <!-- /wp:paragraph -->
Let's run this content through the function
$parse_content = parse_blocks( $content ); print_r( $parse_content );
The function will return the following result:
Changelog
Since 5.0.0 | Introduced. |
parse_blocks() parse blocks code WP 6.7.1
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 ); }