When and Why to Use do_action and add_action in WordPress?

Both the do_action and add_action functions of WordPress are important for creating an action for your WordPress theme or plugin. These functions are mainly used to trigger a code or initiate a code to another segment. It just like an extension to the original code. So using these WordPress hooks, you can add custom code, without having to edit the core code.  

In simple do_action function registers an action hook, while on the other hand add_action function adds a callback function to the registered hook?  In this post, we will see the difference between do_action and add_action function.


As the name implies, do_action is to initiate a function to perform. Placing a do_action()  into your code allows other functions to hook into it to extend the functionality of where that hook is placed, using add_action().

do_action(string $tags, $arg= ‘’)


// Fires once an existing post has been updated.
do_action( 'post_updated', $post_ID, $post_after, $post_before);
// Fires once a post has been saved.
do_action( 'save_post', $post_ID, $post, $update );
//Fires once a post has been saved.
do_action( 'wp_insert_post', $post_ID, $post, $update );


You can even pass arguments to the do_action hook ,

do_action(string $tag, callable $function_to_add, int $priority=10, int $accepted_args=1)

Executes hooked functions when that hook is called at specific points during execution, or when specific events occur. Do_action alone does nothing.  The do_action function sets a label in a specific point in your code for your theme or plugin code to hook into with the add_action function. You can specify the do_action() function in your file where ever it is required either in the header, footer etc. 

Do_action() Example

Do_action(‘hook name’)


Add_action() function is used to tie it into a hook. As the name implies, add_action is used to add the particular function that is hooked. We can hook our own function at a particular point using add_action() function. You can place the add_action() in functions.php file. The general syntax for add_action is

add_action($hook_name, $function_name, $priority, $accepted_args);

Note: This add_action() will add the extra functionalities, that is linked with that function.

Add_action() Example

add_action(‘hook_name’, ‘function name’, 10, 2);

Priority and args are optional. You may or may not pass based on your condition. If you want to know more about actions and filters, you can check out the link https://wpblogx.com/wordpress-hooks/


do_action( 'publish_post', $post_ID, $post, $update ); 

function example_post( $post_ID, $post, $update ) {
   //function for sending an email after publishing the post
   return $post_ID;
add_action( 'publish_post', 'example_post', 10, 3 );


Here, publish_post is an action triggered whenever a post is published, or when the status is changed to publish. So on publishing post, you can use add_action to do some process for your post. Say, sending mail to your users about the post. You can refer here to learn more about WordPress Development.

Hope you understood the concept of do_action and add_action function. If you have any queries or comments, please feel free to comment to us. You can subscribe to us at Facebook and Twitter.

You May also like this post


Leave a Comment