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...

Pre-built websites in BeTheme
Themes

Betheme for WordPress Review

Betheme is a WordPress theme that has been described as one of the most powerful and versatile themes available for WordPress. In this Betheme for

Read More »
WooCommerce for WordPress Review
Ecommerce

WooCommerce for WordPress Review

WooCommerce for WordPress is a great choice for anyone looking to create an online store. Its user-friendly interface makes setup and customization simple, while its

Read More »

Comment

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