1

I'm trying to pass the value of a variable into do_action and then use it in the resulting function but it's not working. The action is being fired in the woocommerce checkout, so it also passes the $checkout variable as well, which is working fine. Simplified example below:

add_action( 'my_action', 'my_function' );
function my_function( $checkout, $myvar ) {
  var_dump ($checkout); //Works fine
  var_dump ($myvar); //Returns NULL
}

And then calling the action in the template like so:

$myvar = 1;
do_action( 'my_action', $checkout, $myvar );

I can't access $myvar at all in the function, any help appreciated.

  • Do not simplify the question, please. It becomes useless as there is no my_action action in WordPress so it's impossible to check. – Max Yudin Mar 8 '17 at 21:38
3

add_action() and add_filter() take 4 params: string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1.

So, the function you are hooking to takes more than 1 param, then just pass the number of params it accepts as the 4th param to add_action(). In your case,

add_action ('my_action', 'my_function', 10, 2) ;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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