How to Check If Shortcode Already Exists in WordPress

Want to check if a shortcode already exists in WordPress? Ever since WordPress 3.6.0 in 2013 you can easily check to see if a shortcode has been registered in WordPress with the shortcode_exists function.

This can be useful if you’re adding functionality to your theme’s functions.php file or creating a specific plugin for your site. Since you don’t want to overwrite existing shortcodes it’s best to check if it already exists before running.

<?php
if ( shortcode_exists( 'gallery' ) ) {
echo 'The [gallery] shortcode already exists';
}
if ( !shortcode_exists( 'gallery' ) ) {
echo 'The [gallery] shortcode does not exist';
}

This is also ideal if you’re removing a plugin and you want to catch instances of an existing shortcode on your site without resulting in an error.

Leave a Reply

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

WordPress Tips Monthly

Get the latest from SmartWP to your inbox.