0

I am trying understanding how do_action_ref_array() works and the difference between do_action() and do_action_ref_array() . I went through (Difference between do_action_ref_array() and do_action()) and get that do_action_ref_array() is not be used now for custom hooks as do_action() is fine . do_action_ref_array() seems to let us pass an array to the action callback however while playing around with it I found that I am only able to access the first element in the action callback and the other values is'nt available in the callback . Consider :

add_action('wp_footer' , 'rb_action_ref_array');
add_action('testing' , 'testing_ref_array');


function rb_action_ref_array(){
    $avar = array(5 , 6) ;
    do_action_ref_array('testing' ,$avar );
}

function testing_ref_array($avar){
  var_dump ($avar) ;

This var_dumps int(5) all though $avar has 5 , 6 . Similarly , if I use the core action filter pre_get_posts , in my callback I see the WP_Query being directly available though I was assuming an array with value of WP_Query should have been passed

add_action('pre_get_posts' , 'rb_modify_posts'); 

function rb_modify_posts($query) {
   var_dump($query); 
} 

Var_dumps object(WP_Query)#321 .

My question is how do I access the other values in the array when do_action_ref_array() is used ?

0

1 Answer 1

1

Whether you use do_action() or do_action_ref_array(), accessing the parameters from the hook callback is just the same for both the functions. But the primary difference between them lies in the way we provide the parameters to the functions:

  • With do_action(), we simply pass each parameter individually:

    do_action( 'some_hook', 1, 2, 3, 'etc' );
    // i.e. do_action( <hook name>, <parameter>, <parameter>, <parameter>, ... );
    
  • But with do_action_ref_array(), we put each parameter into an array and then pass that array as the second parameter to do_action_ref_array():

    do_action_ref_array( 'some_hook', array( 1, 2, 3, 'etc' ) );
    /* i.e. do_action_ref_array( <hook name>, array(
        <parameter>, <parameter>, <parameter>, ...
    ) ); */
    

But despite that difference, do_action_ref_array() will actually pass each of the array items as an individual parameter to the hook callback, i.e. it does something like hook_callback( <parameter>, <parameter>, <parameter>, ... ); e.g. my_function( $args[0], $args[1] ) where my_function is the hook callback and $args is the array passed to do_action_ref_array().

// Let's say you had this:
add_action( 'my_hook', 'my_function' );

// And then this:
$args = [ 1, 2 ]; // same as $args = array( 1, 2 )
do_action_ref_array( 'my_hook', $args );

// And do_action_ref_array would make a call that's similar to:
my_function( $args[0] ); // i.e. my_function( 1 )

// But if you had this: (note the fourth parameter)
add_action( 'my_hook', 'my_function', 10, 2 );

// Then the call would be:
my_function( $args[0], $args[1] ); // i.e. my_function( 1, 2 )

So referring to your code, if you want both the 5 and 6 be available within your callback, then the callback needs to accept two parameters and tell add_action() (via the fourth parameter) to pass two parameters to the callback:

// Set the callback to accept two parameters.
function testing_ref_array( $arg1, $arg2 ) {
    var_dump( $arg1, $arg2 );
}

// And then tell add_action() to pass two parameters to the callback.
add_action( 'testing', 'testing_ref_array', 10, 2 );
// i.e. add_action( <hook name>, <callback>, <priority>, <number of accepted parameters> )

Additionally, it's worth noting that do_action_ref_array() uses call_user_func_array() to call the hook callbacks, so how we call call_user_func_array() is also how we call do_action_ref_array(), except that the first parameter for do_action_ref_array() is the hook name and not the callback. The same context applies to do_action(), but it works more like call_user_func(), despite that do_action() also uses call_user_func_array().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.