To check if a shortcode already exists in WordPress, you can use the has_shortcode() function. This function checks if the specified shortcode exists in the provided content.
Here’s an example of how to use this function:
// How to check if shortcode already exists
// More snippets at wpunplugged.com
if ( has_shortcode( $content, 'my_shortcode' ) ) {
// shortcode exists, do something
} else {
// shortcode does not exist, do something else
}
You can also use the shortcode_exists() function to check if a shortcode has been registered with WordPress. This function takes the shortcode name as an argument and returns a boolean value indicating whether the shortcode exists.
Here’s an example of how to use this function:
// How to check if shortcode already exists
// More snippets at wpunplugged.com
if ( shortcode_exists( 'my_shortcode' ) ) {
// shortcode exists, do something
} else {
// shortcode does not exist, do something else
}
Both of these functions can be used in your theme’s functions.php file or in a plugin.