This site runs best with JavaScript enabled.

Accessing Wordpress blocks data using parse_blocks()

Kevin Beck

October 24, 2019


Having fully accepted Wordpress Gutenberg and made the slow transition over to blocks I will be posting a few things I have learnt on the way.

First up will be to get all blocks added to a post or page using parse_blocks(). Within a post loop:

$blocks = parse_blocks( $post->post_content );
print_r($blocks);

The output of this will be something like:

Array
(
[0] => Array
(
[blockName] => core/paragraph
[attrs] => Array
(
[fontSize] => large
)
[innerBlocks] => Array
(
)

You can then loop over this array and perform various tasks.

$blocks = parse_blocks( $post->post_content );
foreach( $blocks as $block ) {
// Do something
}

Reference

Source