How to check if a shortcode has been registered in WordPress

With WordPress 3.6 comes a few new methods for dealing with shortcodes – like the new has_shortcode() function and the shortcode_atts_{shortcode} filter. However, one thing you can’t do with a simple function is checking if a shortcode has already been registered or not.

There might be a few reasons why you might want to do this. For example, you might be providing a fallback for a certain shortcode, and you don’t want to overwrite it. Whatever the reason, just drop this snippet into your plugin code:

[gist id=2916977]

… and then use the new shortcode_exists( $shortcode_name ) function. Pass a shortcode name as the first parameter, and it will return true if it has been registered, or false if not.

The function_exists() wrapper around the code will make sure that your code doesn’t break if WordPress one day decides to include this function in core, or another plugin is using this code. You might also like to namespace the function or include it in a class.

1 Comment

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.