Search
Close this search box.

How to check if shortcode already exists in WordPress

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.

Share this post

You might also like...

Backup

Jetpack for WordPress Review

Jetpack for WordPress is an amazing plugin that offers a wide range of features to help make your WordPress website more secure, efficient and user-friendly.

Read More »

Comment

Your email address will not be published. Required fields are marked *