1

I need to output some posts on some pages and I created shortcode:

function show_foo() {
    // output stuff    
}
add_shortcode( 'foo', 'show_foo' );

and I use it like this in templates:

echo do_shortcode('[foo]');

But now I'm thinking if I shouldn't use action in favor of shortcode, like this:

function show_foo() {
    // output stuff    
}
add_action( 'foo', 'show_foo' );

and in templates use it like this:

do_action('foo');

What are the cons and pros of these two methods? Do I understand it right that shortcodes should be used only when you need to add attributes to your output like this?

echo do_shortcode([foo param_1="bar" param_2="foobar"]);

And in cases that you need to let the user to put the output of the function into the wysiwyg editor because they don't have access to the templates?

1

Neither are correct. Just use show_foo():

<?php echo show_foo(); ?>

The only reason to ever use shortcodes is to insert dynamic content into a page or post from within the editor (Although, these days the 'proper' way to do that would be with a block, although that's more difficult).

Shortcodes should not be used for outputting content into templates, because parsing the shortcode with do_shortcode() is completely unnecessary overhead.

do_action() is also redundant when outputting code into a template, because you can also just run the callback function directly. The only reasons you'd use do_action() are:

  1. If you wanted to allow other developers to remove the function from the template with remove_action().
  2. You wanted to allow other developers to add their own functions to that specific part of the template.
1

Here's the answer:

  1. Don't use do_shortcode();
  2. 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 show_foo() directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.