1

I am trying to create a shortcode in Wordpress, in which the function that is called with the shortcode tag gets the shortcode tag as parameter.

So say I have

<?php
var $shortcode = 'my_shortcode_tag';
add_shortcode( $shortcode, 'my_shortcode_function');
?>

then I want 'my_shortcode_function' to know the shortcode tag by it was called. I know that I could use attributes as in [my_shortcode_tag tag='my_shortcode_tag'] when I call the shortcode in my actual post, but I want to be able to just write [my_shortcode_tag] and my function to know that it was called by that tag. Is there a way to do this?

1 Answer 1

4

This is sent in as the third argument to a shortcode function (as mentioned in the Shortcodes API).

for example:

add_shortcode( 'shortcode1', 'my_shortcode_function');
add_shortcode( 'shortcode2', 'my_shortcode_function');

function my_shortcode_function($atts, $content, $sc) {
    return $sc;
}

this will output the name of the shortcode called for that function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.