Checking if a specific shortcode is available in content is an easy task to handle while developing WordPress. has_shortcode is the function available in WordPress that developers can use to check if the content has a shortcode or not! in this WordPress tutorial we'll have a closer look at the WordPress has_shortcode function.
has_shortcode
function syntax
The has_shortcode
function gets two parameters, a content and a tag.
Developers use this function to check if a shortcode is available in the content or not.
has_shortcode($content, $tag)
Behind the scenes, WordPress uses regex to find shortcodes in content and checks if a specific shortcode is available between them or not.
But this function does more than just a regex checking.
has_shortcode
also checks if the shortcode is a real shortcode in WordPress or Not.
has_shortcode
function usage example
this function has a simple syntax to use. We’re going to try it in a working example:
if(has_shortcode('[test]', 'test')){
// test is not a default WP shortcode
// if you or a plugin did not defined it, will return false
}
if(has_shortcode('', 'video')){
// test is a default WP shortcode
// if you did not removed it, this condition is true
}
If you are interested in shortcodes in WordPress, you can read my tutorials:
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Plugin Development WordPress Theme Development
What do you think about "How to Check if Content has a Specific Shortcode in WP"?