The difference between do_action() and add_action() in Wordpress

Viewed 924

After reading multiple online posts about do_action()/add_action(), I would like to brief my understanding about them.

Here I would only talk about action hooks, not about the filters.

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

Now the questions are :

1> About add_action()/do_action() for existing hook :

add_action( 'admin_enqueue_scripts', 'my_func');

In the above example the callback function my_func() is registered with the hook 'admin_enqueue_scripts'. Now whenever do_action('admin_enqueue_scripts') is called somewhere inside the code in future, the callback function my_func() would be executed. So in brief, the callback function won't be executed unless do_action() is called. Is my understanding correct ?

2> About add_action()/do_action() for custom hook:

Now consider a situation I am writing a new plugin and I want to provide an action hook inside it. In this case how these two functions would be used ?

// inside my plugin code
do_action('my_custom_hook');

// inside the user code who wants to use this hook
function  user_func()
{
  // code ...
}
add_action('my_custom_hook', 'user_func');

Is my understanding correct ?

0 Answers
Related