Function Name: has_shortcode
If you’re building a WordPress theme or plugin, you may want to check if a specific page or post has a shortcode in its content. That’s where the has_shortcode function comes in handy.
The has_shortcode function lets you check if a shortcode exists in the content of a post or a page. It returns a boolean (true or false) value depending on whether the shortcode is found or not.
Here’s an example usage code:
Let’s say you want to display a specific message if a post has a shortcode for a contact form. Here’s how you could use the has_shortcode function:
if ( has_shortcode( get_the_content(), 'contact-form-7' ) ) {
echo 'This post has a contact form.';
} else {
echo 'This post does not have a contact form.';
}
In this example, we use the get_the_content() function to retrieve the post content and pass it as the first parameter to the has_shortcode function. The second parameter is the name of the shortcode we’re looking for ("contact-form-7" in this case). The function will return true if it finds the shortcode, and false otherwise.
Overall, has_shortcode is a useful function that can help you determine whether a post or page contains a specific shortcode.