Introduce alternative to do_shortcode( '[shortcode]' )

Description

There are times when we want to call a shortcode programmatically. Currently, the easiest way to do this is do_shortcode( '[shortcode]' ). But that does lots of unnecessary work. A developer has two alternatives:

Manually search through the source and find the function that handles the shortcode, and call it directly. The only problem is that this doesn't offer a very forward-compatible solution, especially when the shortcode is being offered by a plugin.

Search through $shortcode_tags to find and call the function. That's more forward-compatible, but it kind of seems hacky.

I'd like to request that we offer a core function that does number 2 rather than each developer having to implement it themselves.

So for instance, the [gallery] shortcode callback is gallery_shortcode(), and you can do:

echo gallery_shortcode( $attr );

Yes, I know that and mentioned it above, but it becomes a bigger issue when dealing with plugins, which may change the function name without notice. I guess maybe that's an edge case though?

My thinking was that if we make people search through the source looking for the right function to call, they'll just call do_shortcode( '[shortcode]' ) instead. If we can point them to an easy to use core function, they'll probably use it.

I think this is the best solution to the problem, but if you don't think it's a real problem, or isn't big enough to require this solution, I'll respect that.

It's certainly possible to call the callback directly if you know it, but that's not necessarily the case. Shortcodes are pretty fluid, and it's possible that a plugin could change the gallery shortcode (e.g.) to its own instead.

The current solutions are either extremely heavy (do_shortcode) or require an obscure way to call them (do_shortcode_tag).

Not sure what you mean by that. If you are using nested shortcodes, then you should be using do_shortcode() instead of this, or calling the nested ones like this before passing the the result in as the $content.

Nested shortcodes shouldn't be a problem -- they would just call do_shortcode() like you do now. This just allows skipping calling do_shortcode() when you know what the shortcode/content is, unlike a nested shortcode.

Since this was in "600 entries in over 270 plugins"* back in 2013 (and is almost certainly worse now) it would seem like a better way forward would be to tweak do_shortcode() to detect the case of a single shortcode being called, and if so, run do_shortcode_tag() directly. That avoids the overhead of scanning the entire $shortcode_tags array without adding a new function or requiring hundreds of plugins to be updated.

I know shortcodes in general are up for a rebuild, but this seems like some very low hanging fruit that could improve performance across hundreds of plugins (especially for sites with large numbers of registered shortcodes) in the meantime.

Rewrote the patch. Since this is intended for internal use, it now returns a WP_Error on invalid arguments. (If you're doing output and would rather ignore stuff, probably best to call do_shortcode still.)

If @nickciske's suggestion sounds OK, we could probably do it, but I think it's more trouble than it's worth. It'd introduce a fair bit of complexity into do_shortcode for an edge case that's better handled by a separate function, IMO.

Testing shows it comes out barely faster than do_shortcode, but calling it is a fair bit nicer for internal use: