Here's the answer:
- Don't use
- For all other cases, refer to #1.
If you're working in an area where you can use PHP, there's no need to use
do_shortcode() since you can run the function directly.
do_shortcode() runs a pretty intensive regex as it has to run through (and parse) ALL shortcodes on the site, so don't use it when it is unnecessary.
(If you're adding shortcode parsing to an area within a theme, then that's a different story, but for most cases, don't do it!)
Yes, in some cases, the shortcode's function is not direct and might be part of a class, so here's a good solution when needing to access a shortcode that way.
Your question about
do_action() actually seems like you may not understand the action hook concept. Adding
do_action( 'my_hook' ) is adding an action hook in your template (or wherever you add this).
There is a common misuse of "action hook" (and "filter hook") when discussing these terms. Many people will write a function hooked to an action and call that "the hook," but the "hook" is the
do_action() (or the
apply_filters()). The function you write is "hooked to" that action. (More correctly, this is the "callback".)
So in your template where you've put
do_action('foo') you have created an action hook at that point. This is a point for customization. If you're doing this in a theme that only you will use in a single instance, there's no need for this. You only need this if you're writing it for public use OR if you use your framework across several sites where you might apply some custom element.
The correct approach based on what you described is to simply use