Organizing shortcodes. How to display all of them and their attributes?

The question:

1. My shortcodes

I’ve developed multiple shortcodes for my own theme, I’m loading them in functions.php like this require_once (MY_URL . '/bartag_shortcode.php').

Here’s an exemplary bartag shortcode from WordPress Codex:

function bartag_func( $atts ) {
    extract( shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts ) );

    return "foo = {$foo}";
add_shortcode( 'bartag', 'bartag_func' )

2. How to list all of them?

  • I’m not even sure if I can do that, since the add_shortcode() doesn’t allow to group shortcodes, so I guess it will be pretty hard to distinguish my shortcodes from the others (from plugins, or other sources). Any ideas? I could loop trough all of shortcodes containing files using foreach(glob('*.php') as $shortcode) and file_get_contents with some regex, but when it comes to performance this is one of the dumbest ideas I’ve ever came up with.
  • If that’s not possible then how to display a list of all active shortcodes, so I could filter them somehow (I could add a prefix to the ones created by me and then list all shortcodes starting with this prefix).


The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Inspect the global variable $shortcode_tags:

print '<pre>' . htmlspecialchars( print_r( $GLOBALS['shortcode_tags'], TRUE ) ) . '</pre>';


     => img_caption_shortcode
     => img_caption_shortcode
     => gallery_shortcode
     => __return_false
    [contactform] => Array
            [0] => T5_Contact_Form Object
                    [debug:protected] => 
                    [base_name:protected] => t5-contact-form/t5-contact-form.php
                    [prefix:protected] => t5c
                    [address:protected] => 
                    [nonce_name:protected] => t5_contact_form_nonce
                    [hidden_field:protected] => t5_no_fill
                    [option_name:protected] => t5c_default_address

            [1] => shortcode


As you can see you get the shortcode name as key and the associated function as the value. If the function is an object you get the object with its properties and the associated function as an array.

I don’t see a way to get the default attributes. Maybe per Reflection API.

The attributes are defined inside of the functions. The registered shortcode doesn’t know anything about the inner workings of the associated function. And the default attributes can be ambiguous: they might be the result of another function call inside of the callback handler.

Method 2

Take a look at Shortcode Reference which is a plugin that provide a list of all available shortcodes using the PHP’s Reflection API and that should give you an idea.

Other then that you can provide a UI for using your shortcodes, for example take a loot at my plugin ShortCodes UI Which creates a Dialog for each shortcode dynamically with the needed attributes.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment