Pros and cons of actions over shortcodes

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.

Leave a Comment