Get shortcode name from within it’s callback function?

I’m dynamically generating shortcodes with my plugin like this:

foreach ($options['grids'] as $grid) {
    add_shortcode($grid['shortcode'], array($this, 'print_shortcode'));
}

But the problem is, in the callback function I need to print different content, based on the shortcode’s name.

Read More

My question is – how would I get the shortcode’s name from within print_shortcode() ?

Thank you!

Related posts

Leave a Reply

1 comment

  1. The shortcode’s name is referred to as the tag. The tag is actually the 3rd (and last) parameter passed to the shortcode’s callback function.

    So your callback should look something like this:

    function print_shortcode($atts, $content, $tag) {
        // $atts - array of attributes passed from shortcode
        // $content - content between shortcodes that have enclosing tags eg: [tag]content[/tag]
        // $tag - shortcode name
    
        // do something... perhaps:
    
        switch($tag) {
            case 'dog':
                // some code
                break;
    
            case 'cat':
                // some code
                break;
    
            case 'asparagus':
                // some code
                break;
        } // switch
    }