Note: the following example is wrong*. Do not do it!
I often see code like the following:
do_shortcode( '[some_shortcode]' );
I want to explain the proper way of manually running shortcodes using PHP.
*Ok, it’s not strictly wrong, it’s just not the best solution all the time.
Sal Ferrarello says
If I understand properly you’re advocating calling the underlying function/method instead of calling
do_shortcode()
to avoid the extra overhead of the regular expression processing of the string that gets passed todo_shortcode();
. I agree with this.As you mentioned in your tweet, this is definitely better when you’re in control of the shortcode (i.e. it is your own code) though a little less certain when the shortcode is someone else’s (they could change the name of their function/method).
I’m spitballing here without really thinking it through but it seems one could use the shortcode string to lookup the callable function in the global
$shortcode_tags;
and then call that function – that could be cool.Jeremy says
Generally speaking, I would indeed advocate for calling the underlying function/method directly where possible. Using the global
$shortcode_tags
is an idea I hadn’t thought of before, but that’s another good avenue to take.Thanks for the feedback, Sal 🙂