WordPress at a glance

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.000001s = speed of light | 50000 times = 0.04s = 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: wp-includes/blocks.php WP 5.2.2

<?php
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:' );
}

Related Functions

From category: Other

No comments
    Hello, !     Log In . Register