This question might be wrong, I am not sure about it. Because I am not clear on this.

I know add_action it is used to to hook our function to the specified function. For example add_action('wp_head'.'myfunc'); now what ever code in myfunc will be executed in the wp_head(). This is clear but i am having doubt in do_action what it does?

I think it is used to create our own hook like already available hooks(wp_head,wp_footer,..etc) If i am correct can anyone show me a simple understandable answer with simple example.

I have tried the difference in internet but all are pointing to difference between add_action and add_filter. I don't want to go there because first i want to clarify this and then I’ll move there.

Can anybody help me?

EDIT after Question POST

function custom_register()
    echo '<script>jQuery(document).ready(function(){alert("Learning Hooks");});</script>';



I tried this in plugin but i didn't get the alert message.

But when i hook the same function with wp_head then it is working fine

  • What happens when you call do_action('custom'); after you registered the callback? And make sure jQuery is loaded. – fuxia Oct 28 '13 at 11:07
  • i did not get the alert message either on back end or front end – sun Oct 28 '13 at 11:12
  • jQuery are loaded. When i hook the same function with wp_head it is working fine but not with the custom – sun Oct 28 '13 at 11:28
  • Sun, I believe your original Question was answered by toscho and you should mark this as resolved. The edits #3 an #4 that you've done belong on a new Question. – brasofilo Oct 30 '13 at 13:32

Use do_action( 'unique_name' ) to create your own actions.

You can use that to offer an API for your plugin, so other plugins can register callbacks for your custom action. Example: Do I need to call do_action in my plugin?

But you can use custom actions (or filters) in a theme too. Example: Best practice way to implement custom sections into a WordPress theme

And you can combine both to make a plugin and a theme working together. Example: How to make method from plugin available in theme?

Summary: add_action( 'foo' ) registers a callback, do_action( 'foo' ) executes that registered callback.

| improve this answer | |
  • Thanks for the help i got this is mainly for API purpose. I tried a sample but it didn't work out.Can you find any problem there? Check my EDIT @toscho – sun Oct 28 '13 at 11:07
  • Can we say this as another case like inheritance? intead of extending and overriding the class we are going this way. Is this correct? – sun Oct 28 '13 at 12:11
  • @sun Hm, yes, but you can combine both approaches, they are not mutually exclusive. – fuxia Oct 28 '13 at 12:19

This is my guess, so if you know better, please make a comment so I can update my guess.

Your plugin code is executed before wp_head() (which we can assume will invoke the actions added to it). When you add_action('wp_head','custom_register'), you are telling PHP that when (in the future) do_action('wp_head') is called, to call custom_register() as well. The same is true of your call to add_action('custom','custom_register') but as you see in your code, the call to do_action('custom') has already been made, and when it was called, there was not (yet) any action added to it. This is why Toscho asked what happens when you call do_action('custom') after you registered the callback. Your answer about back end and front end is ambiguous. If you swap the last two lines in the following code, I think it will work:

function custom_register()
    echo '<script>jQuery(document).ready(function(){alert("Learning Hooks");});</script>';

do_action('custom');                    // This is called before it will have an effect.

add_action('custom','custom_register'); // Too late - do_action was already called.
| improve this answer | |

do_action : Registers an action hook while add_action : adds a callback function to the registered hook.


Consider you wanted to print something before sidebar in you template.

  1. You will add an action hook in your template file index.php via <?php add_action('bp_sidebar_left'); ?>.
  2. Now in your functions.php file you can add a callback function to that hook to print something you want.

add_action('bp_sidebar_left', 'bp_sidebar_left_cb'); function bp_sidebar_left_cb() { echo 'Hello World !'; }

| improve this answer | |

You have to use like below:

function custom_register()
    echo '<script>jQuery(document).ready(function(){alert("Learning Hooks");});</script>';


add_action('custom','custom_register'); // Too late - do_action was already called. 
do_action('custom');                    // This is called before it will have an effect
| improve this answer | |
  • It is working for me – user112289 Feb 1 '17 at 16:16

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