I would like to create shortcodes, but some tutorials write about to use add_action:

add_action( 'init', 'register_shortcodes');

But I read another tutorial where the author didn't use it, only put this to functions.php:

add_shortcode( 'recent-posts', 'recent_posts_function' );

Which is the best method, and why to use add_action?


If you take a look at the Codex page about add_shortcode() you won't see anything about the need of an add_action() before you can use add_shortcode().

So, you can just put your add_shortcode() directly into your functions.php. This is what I do too.

See this article - WordPress Shortcodes: A Complete Guide about the use of shortcodes and best practices.

  • 3
    You have actuaclly linked to an article saying you should wrap in an action. – raam86 Sep 1 '13 at 13:39

Sometimes it is required to use 'init' action. For example, right now I edit theme heavily based on widgets and customize panel. I tried to add custom shortcode without actions and got errors upon saving content in Customize panel. Action ensures the right order of executing functions. It says "hey shortcodes! First I will load most important functions, then you!"

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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