Reputation: 1492
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?
Upvotes: 1
Views: 2936
Reputation: 9638
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.
Upvotes: 4