WordPress, WordPress Development Tutorials

add_action in WordPress with save_post hook or action example

Written by Anup Kumar
Posted on - 4 min read

In simple words, add_action in WordPress is used to add callbacks and the beauty of add_action is you can add multiple callbacks against a single hook and sort them according to priority.

Let’s understand things one by one to make it easier to understand.

Below is the common syntax for procedural programming way

add_action( $hook_name, $callback, $priority = 10, $accepted_args = 1 );
  1. $hook_name name of the hook let’s say save_post
  2. $callback function that you want to call when save_post hook will be fire
  3. $priority default is 10, you can give below 10 to invoke early and greater than 10 to invoke later.
  4. $accepted_args number of arguments that you want in your callback. it can’t be greater than number of arguments supported by the respective hook. and the default is 1.

Let’s understand this with a very basic example with a hook “save_post”. So save_post support a maximum of 3 arguments. if you dig WordPress you’ll find below line from it get fired or you can say register as a hook/action.

save_post hook or action

Fires once a post has been saved. You can receive three parameters in callback using save_post hook or action.

do_action( 'save_post', int $post_ID, WP_Post $post, bool $update );
// for more https://developer.wordpress.org/reference/hooks/save_post

Example with 1 argument of save_post hook or action

So let’s hook our custom function ‘filter_nude_pics‘ after the post has been saved. Which will filter the author who can’t upload nude pics for quality check.

add_action('save_post', 'filter_nude_pics'); 
// so $priority is 10 & $accepted_args = 1 as default value 
//add_action('save_post', 'filter_nude_pics', 10, 1); is same as above 

functionfilter_nude_pics($arg1){
   // here $arg1 will $post_ID. You can't alter it. means you can't receive 2nd directly. 
}

Example with 2 arguments of save_post hook or action

What if I want to access the second params. I can write it like below?

add_action('save_post', 'filter_nude_pics'); 
// so $priority is 10 & $accepted_args = 1 as default value

functionfilter_nude_pics($arg1, $arg2){
   // ideally $arg2 must be WP_Post $post
}

But above line will give you an error Fatal error: Uncaught ArgumentCountError: Too few arguments to function … because we asked to return 1 argument in the callback and trying to receive two.

So the correct version of the above code will be:

add_action('save_post', 'filter_nude_pics', 10, 2);  // as we need two args

functionfilter_nude_pics($arg1, $arg2){
    // now $arg1 will be int $post_ID & $arg2 will be WP_Post $post
}

Example with 3 arguments of save_post hook or action

The last and 3rd arguments of save_post hook $update tell us – the current hook is for a new post or for an existing post. If you are saving a post the first time. It returns false as it is inserting not updating else it returns true. to receive all three parameters you can use the following code snippet.

add_action('save_post', 'filter_nude_pics', 10, 3);  // as we need three args

functionfilter_nude_pics($arg1, $arg2, $arg3){
    // now $arg1 will be int $post_ID & $arg2 will be WP_Post $post & $arg3 will be $updated (if true then update operation else insert
}

Make sure I’m using params name as $arg1, $arg2, $arg3 is not the correct way of naming conversion. It was just to explain. The correct way is:

add_action('save_post', 'filter_nude_pics', 10, 3); 
functionfilter_nude_pics($post_id, $post, $is_updated){
   // body of function
}

Using add_action in classes

Only the add_action callback function gets changed else everything remains the same.

// for class based
add_action( $hook_name, array($cls_obj_ref, $callback), $priority = 10, $accepted_args = 1 );

// e.g 

class ContentQualityControl{
  public function __construct($plugin_name, $version){add_action('save_post', array($this,'filter_nude_pics'), 10, 3); 
  }  public functionfilter_nude_pics($post_id, $post, $is_updated){
     // body of function
  }    
}

Changing the priority in add_action

sometimes save hook or action is being called from many plugins or for multiple tasks. But you want to perform your function at the earliest of at extreme end. you can play with priorities.

here is the best example to clear the above paragraph.

add_action('save_post', 'callback_one', 30, 3);add_action('save_post', 'callback_two', 9, 3);add_action('save_post', 'callback_three');add_action('save_post', 'callback_four', 33, 3);

so it will be exeucted in following order:callback_two having 9 prioritycallback_three having default 10 priority
callback_one having 30 priority
callback_four having 33 priority

So that’s it.

chat-box