Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Do I need to call do_action function every time I add_action something?

share|improve this question
1  
Why do you think you should? Example code? –  toscho Sep 10 '12 at 9:24
    
I was exploring the codes of WooCommerce plug-in, they've called do_action a lot of time. I'm new to Wordpress and thinking of building a plug-in, so I was checking out some existing codes. –  giga Sep 10 '12 at 9:27

1 Answer 1

up vote 3 down vote accepted

Usually, you use existing action in your plugins. WordPress will call do_action() when needed.

If your plugin is rather complex, like a shopping plugin, you may want to offer additional actions for other plugin developers or for the pro-version of your plugin. In these cases you add do_action() wherever you think it could be useful.

An example: I have a library of PHP classes that I use for some projects. When I write a plugin that relies on my library I have to wait until the library is ready. So I write in my library:

require 'class.T5_Custom_Post_Type_Helper.php';
require 'class.T5_Custom_Taxonomy_Helper.php';
// and so on…
do_action( 't5_library_loaded' );

In my other plugin I hook into this action to start all the work:

add_action( 't5_library_loaded', array ( 'My_Plugin_Class', 'init' ) );

Now I know all needed classes available, I don’t have to test class_exists() each time.

add_action() and do_action() are not necessary tied together. So the answer to your question is: no. :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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