actions - Why are you using add_action for shortcode?

admin2025-06-03  3

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?

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?

Share Improve this question edited Jan 13, 2016 at 0:31 jgraup 9,9143 gold badges32 silver badges70 bronze badges asked Mar 13, 2013 at 8:22 RolninRolnin 1191 gold badge1 silver badge6 bronze badges 1
  • Wrapping the calls to add_shortcode in a function and calling that function with a hook gives you more control of when the shortcodes are added. For example, you may want to add those shortcodes when a specific plugin is activated, but not otherwise. – admcfajn Commented Feb 20, 2019 at 6:26
Add a comment  | 

3 Answers 3

Reset to default 4

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.

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!"

Without repeating too much that has been covered in the answers above; this is just a best practices approach. I often use this in plugin development where I need to have separate classes like in the wppb.me plugin starter template.

It makes sense to register all your admin hooks in the loader. In the case of the shortcode, you can add the add_action shortcode hook in the hooks loader method :

 /**
   * Register all of the hooks related to the admin area functionality
   * of the plugin
   */ 

     private function define_admin_hooks() {

     $plugin_admin = new instance( $this->get_plugin_name(), $this->get_version() );

     $this->loader->add_action( 'init', $plugin_admin, 'register_shortcodes');
}

The main benefit I have with this approach is for easy code maintenance since the code is modular. You can add several other hooks in the same method which makes it easy to track and change things, especially for a large plugin.

It, therefore, means in the class where you want to use add_shortcodeyou do not have to run the action hook in the constructor method, you can now use it in a custom method like :

public function register_shortcodes(){

        add_shortcode('recent-posts', array($this, 'recent_posts_function'));

    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748888007a314554.html

最新回复(0)