Checking for specific blocks in WordPress using has_block
The has_block
function in WordPress is a function that checks if a specific block type is present in the content. It is a part of the WordPress Block API which was introduced with the Gutenberg editor in WordPress 5.0. The function is used to determine if a post or a page contains a specific block type.
This function can be useful in various scenarios. For example, it can be used to conditionally load scripts or styles only when a particular block is present in the content. This can help in optimizing the loading time of a website by preventing unnecessary loading of scripts and styles. It can also be used to alter the output of a post or a page based on the presence of a specific block.
The has_block
function returns a boolean value – true if the block type is present in the content and false if it is not. This makes it easy to use in conditional statements in the code.
It’s worth noting that this function only checks for top-level blocks. If a block is nested inside another block, this function will not be able to detect it.
Parameters Accepted by the has_block Function in WordPress
The has_block
function in WordPress accepts two parameters:
$block_name
(string) – This required parameter specifies the complete block type that the function should search for.$post
(int|string|WP_Post|null) – This optional parameter can be the post content, post ID, or post object. If no value is provided, it defaults to the global$post
.
Return Value of the has_block Function
The has_block
function returns a boolean value. This value indicates whether or not the post content contains the specific block type that was passed to the function as a parameter.
If the function does not receive any parameters, it simply returns a boolean value indicating whether or not the post content contains any blocks.
Examples
How to Check if a Specific Block Exists in a Post Content
if ( has_block( 'core/image', get_the_content() ) ) {
echo '<p>The post contains an image block.</p>';
} else {
echo '<p>The post does not contain an image block.</p>';
}
This code snippet checks if an image block (‘core/image’) exists in the content of the current post. If the image block exists, it prints out a message saying “The post contains an image block.”. If it doesn’t, it prints “The post does not contain an image block.”.
How to Check if a Post Contains a Custom Block
if ( has_block( 'my-plugin/my-custom-block', get_post( $post_id ) ) ) {
echo '<p>The post contains a custom block from my plugin.</p>';
} else {
echo '<p>The post does not contain a custom block from my plugin.</p>';
}
In this example, the has_block
function is used to check if a specific post identified by $post_id
contains a custom block (‘my-plugin/my-custom-block’). If the custom block exists, it prints out a message saying “The post contains a custom block from my plugin.”. If it doesn’t, it prints “The post does not contain a custom block from my plugin.”.
How to Check if a Block Exists in a String of Content
$content = 'This is a test content with a <!-- wp:core/paragraph -->paragraph block<!-- /wp:core/paragraph -->.';
if ( has_block( 'core/paragraph', $content ) ) {
echo '<p>The content contains a paragraph block.</p>';
} else {
echo '<p>The content does not contain a paragraph block.</p>';
}
In this example, the has_block
function is used to check if a paragraph block (‘core/paragraph’) exists in a string of content stored in the $content
variable. If the paragraph block exists, it prints out a message saying “The content contains a paragraph block.”. If it doesn’t, it prints “The content does not contain a paragraph block.”.
Conclusion
The has_block
function in WordPress is a tool that checks if a specific block type is present in the content. This function is predominantly used in themes and plugins to conditionally display content or implement specific functionalities based on the presence or absence of certain block types. By utilizing the has_block
function, developers can create more dynamic and responsive WordPress sites that adapt based on the content structure.