The do_blocks() WordPress PHP function parses dynamic blocks out of post_content
and re-renders them.
Usage
To utilize the do_blocks() function, pass the post content as a string. Here’s an example:
$post_content = 'This is a post with a [shortcode]'; $rendered_content = do_blocks($post_content); echo $rendered_content;
In this example, the function will find the dynamic blocks inside the $post_content
, re-render them, and then return the re-rendered content.
Parameters
- $content (string) – The post content that needs to be parsed for dynamic blocks.
More information
See WordPress Developer Resources: do_blocks()
This function is part of the WordPress core and is not deprecated as of the latest version.
Examples
Basic Usage
$post_content = 'Hello, World! [block]'; $rendered_content = do_blocks($post_content); echo $rendered_content;
This code will parse any dynamic blocks inside the $post_content
and output the re-rendered content.
With a Custom Block
$post_content = 'Welcome to my website [custom_block]'; $rendered_content = do_blocks($post_content); echo $rendered_content;
This will parse and render the ‘custom_block’ in the given $post_content
.
With Multiple Blocks
$post_content = 'This is a post with [block1] and [block2]'; $rendered_content = do_blocks($post_content); echo $rendered_content;
This code will find both ‘block1’ and ‘block2’ in the $post_content
, render them, and output the result.
No Blocks Present
$post_content = 'Just a regular post content without any blocks'; $rendered_content = do_blocks($post_content); echo $rendered_content;
Here, since there are no dynamic blocks in the $post_content
, the function will simply return the original content.
Nested Blocks
$post_content = 'This is a post with a [parent_block [child_block]]'; $rendered_content = do_blocks($post_content); echo $rendered_content;
In this example, the function will parse and render both the ‘parent_block’ and the nested ‘child_block’ from the $post_content
.