has_blocks() WP 1.0
Determine whether a post or content string has blocks.
This test optimizes for performance rather than strict accuracy, detecting the pattern of a block but not validating its structure. For strict accuracy, you should use the block parser on post content.
1 time — 0.000001 sec (speed of light) | 50000 times — 0.04 sec (speed of light) | PHP 7.2.5, WP 5.0
No Hooks.
Return
true/false. Whether the post has blocks.
Usage
has_blocks( $post );
- $post(int|string|WP_Post|null)
- Post content, post ID, or post object.
Default: global $post
Notes
- See: parse_blocks()
Changelog
Since 5.0.0 | Introduced. |
Code of has_blocks() has blocks WP 5.6.2
function has_blocks( $post = null ) {
if ( ! is_string( $post ) ) {
$wp_post = get_post( $post );
if ( $wp_post instanceof WP_Post ) {
$post = $wp_post->post_content;
}
}
return false !== strpos( (string) $post, '<!-- wp:' );
}