On this page
has_blocks( int|string|WP_Post|null $post = null ): bool
Determines whether a post or content string has blocks.
Description
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.
See also
Parameters
$postint|string|WP_Post|null Optional-
Post content, post ID, or post object.
Defaults to global $post.Default:
null
Return
bool Whether the post has blocks.
Source
File: wp-includes/blocks.php. View all references
function has_blocks( $post = null ) {
if ( ! is_string( $post ) ) {
$wp_post = get_post( $post );
if ( ! $wp_post instanceof WP_Post ) {
return false;
}
$post = $wp_post->post_content;
}
return false !== strpos( (string) $post, '<!-- wp:' );
}
Related
Uses
| Uses | Description |
|---|---|
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| block_version() wp-includes/blocks.php | Returns the current version of the block format that the content string is using. |
| do_blocks() wp-includes/blocks.php | Parses dynamic blocks out of |
| has_block() wp-includes/blocks.php | Determines whether a $post or a string contains a specific block type. |
| _WP_Editors::parse_settings() wp-includes/class-wp-editor.php | Parse default arguments for the editor instance. |
Changelog
| Version | Description |
|---|---|
| 5.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/has_blocks