WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/do action ref array

Description

Execute functions hooked on a specific action hook, specifying arguments in an array.

This function is identical to do_action, but the arguments passed to the functions hooked to $tag are supplied using an array.

Usage

 <?php do_action_ref_array$tag$args ); ?> 

Parameters

$tag
(string) (required) The name of the action to be executed.
Default: None
$args
(array) (required) The arguments supplied to the functions hooked to $tag
Default: None

Example

Call an action and pass an array of arguments:

$args = array( 'arg_1', true, 'foo', 'arg_4' );

do_action_ref_array( 'my_action', $args );

This is the same as:

do_action( 'my_action', 'arg_1', true, 'foo', 'arg_4' );

Notes

  • This function can be useful when your arguments are already in an array, and/or when there are many arguments to pass. Just make sure that your arguments are in the proper order!
  • Before PHP 5.4, your callback is passed a reference pointer to the array. Your callback can use this pointer to access all the array elements. Adding an action and declaring a call back that hooks the above example action could look like this:
add_action('my_action', 'my_callback');
function my_callback( $args ) {
    //access values with $args[0], $args[1] etc.
}

Because the array was passed by reference, any changes to the array elements are applied to the original array outside of the function's scope.

  • Regardless of PHP version, you can specify the number of array elements when adding the action, and receive each element in a separate parameter in the callback function declaration like so:
add_action('my_action', 'my_callback', 10, 4 );
function my_callback( $arg1, $arg2, $arg3, $arg4 ) {
    //access values with $args1, $args2 etc.
}

This method copies the array elements into the parameter variables. Any changes to the parameter variables do not affect the original array.

  • As of PHP 5.4, the array is no longer passed by reference despite the function's name. You cannot even use the reference sign '&' because call time pass by reference now throws an error. What you can do is pass the reference pointer as an array element. Doing so does require all callbacks added to the action to expect a reference pointer. This is not something you will see in WordPress actions. This technique is provided for informational purposes only.
do_action_ref_array( 'my_action', array( &$args ));

add_action('my_action', 'my_callback');
function my_callback( &$args ) {
    //access values with $args[0], $args[1] etc.
}

Because the original array was passed by reference, any changes to the array elements are applied to the original array outside of the function's scope.

Change Log

Since: 2.1.0

Source File

do_action_ref_array() is located in wp-includes/plugin.php.

Related

Actions: has_action(), add_action(), do_action(), do_action_ref_array(), did_action(), remove_action(), remove_all_actions()

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.