add_action Additional Parameters

add_action() requires a minimum of 2 parameters. It also accepts 2 optional additional parameters:

  • int $priority for the priority given to the callback function,
  • int $accepted_args for the number of arguments that will be passed to the callback function.

Priority

Usually when many callback functions are hooked to a single action hook you need to tell WordPress which callback functions should execute before or after which callback functions. WordPress determines the order of running the callback functions based on two things:

  • value of the $priority parameter passed as the third argument to add_action() function.
  • order in which the callbacks were registered if they have the same priority value.

Priority values are positive integers, usually between 1 and 20. The default priority value, when no priority value is given, is 10. There is no theoretical upper limit on the priority value, but the realistic upper limit is 100. A function with a lower priority value will run before a function with a higher priority value. This means that a function with a priority of 3 will run after a function with a priority of 2. However, if two callback functions are registered for the same hook with the same priority, they will be run in the order that they were registered to the hook.

For example:

<?php
add_action('init', 'sdc_callback_example_one', 23);
add_action('init', 'sdc_callback_example_two');
add_action('init', 'sdc_callback_example_three', 8);
add_action('init', 'sdc_callback_example_four', 23);
?>
  • sdc_callback_example_three() will execute first because it has the lowest priority of 8
  • followed by sdc_callback_example_two() because it has a default priority of 10 since it’s priority is not explicitly set
  • sdc_callback_example_one() and sdc_callback_example_four() have the same priority so sdc_callback_example_one() will run first because it was hook earlier.

Number of Accepted Arguments

There are times when a callback function should receive some extra data related to the action being hooked to. Such as when WordPress saves a post and runs the save_post action hook. It passes two parameters to the callback function:

  • the ID of the post being saved
  • the post object itself.
<?php
do_action( 'save_post', $post->ID, $post );
?>

In this case, when your callback function is registered for the save_post hook, you can specify that it will receive those two parameters passing the value of the fourth argument as 2:

<?php
add_action('save_post', 'sc_sdc_custom', 3, 2);
?>

You can then prepare to receive those parameters in your callback function by modifying the parameters your callback function will accept, like this:


function sc_sdc_custom_post( $post_id, $post ) {
    // do something
}

Example on usage of priorities

Let us edit our previously created plugin by adding more callback functions with specific priorities with and see the output.


<?php
/*
Plugin Name: Sytech Footer Message
Version: 1.0
Description: Adds a simple text to the footer.
Author: Sydney Chako
Author URI: https://sytech.co.zw
Plugin URI: https://sytech.co.zw/footer-message/
*/

add_action('wp_footer', 'sdc_callback_example_one', 23);
add_action('wp_footer', 'sdc_callback_example_two');
add_action('wp_footer', 'sdc_callback_example_three', 8);
add_action('wp_footer', 'sdc_callback_example_four', 23);

function sdc_callback_example_one(){
	echo 'Hi, I am sdc_callback_example_one()<hr>';
	}

function sdc_callback_example_two(){
	echo 'Hi, I am sdc_callback_example_two()<hr>';
	}

function sdc_callback_example_three(){
	echo 'Hi, I am sdc_callback_example_three()<hr>';
	}

function sdc_callback_example_four(){
	echo 'Hi, I am sdc_callback_example_four()<hr>';
	}

?>

This time your footer will look like this:

add_action Additional Parameters