Wordpress - Using wp_schedule_single_event with arguments to send email
Thanks to Rarst this makes a hell of a lot more sense now. So I've updated this post to elaborate the differences Rarst mentioned and upvoted his for shedding light on this ; )
Basically wp_schedule_single_event passes the arguments to your function through the variable args as shown in the codex. This variable "args" must be an array because each value in the array will be mapped to an argument in your call back function.
Example:
add_action('example_action', 'example', 1, 3);
$args = array ( 2, 1, 3 );
wp_schedule_single_event(time(), 'example_action', $args);
function example($a, $b, $c)
{
}
2 will go to $a, 1 will go to $b and 3 will go to $c. However passing three variables is only possible because of this line,
add_action('example_action', 'example', 1, 3);
Looking at the codex for add_action you see that the fourth argument, 3, is what controls how many arguments are passed to the call back function. The default is 1.
So this example also works:
add_action('example_action', 'example');
$args = array ( array( 2, 1, 3 ) );
wp_schedule_single_event(time(), 'example_action', $args);
function example($a)
{
}
So here the array( 2, 1, 3) is assigned to just $a.
So Sardine's issue could be resolved with one line change where line 7,
$args = array('email' => $email, 'title' => $post->post_title);
becomes this instead,
$args = array(array('email' => $email, 'title' => $post->post_title));
I think you have mismatch in how you pass arguments and how you expect it to work. You pass array of arguments to schedule and expect your hooked function to receive identical array of arguments. This is not the case.
Cron events are processed by do_action_ref_array()
, which in turn passes arguments via call_user_func_array()
.
So your hooked function does not receive array of arguments, it receives multiple arguments - one for each element in your array.
So you need to either wrap array of arguments in array one more time or modify your function to process multiple arguments. Note that for letter you will also need to modify your add_action()
call so that required number of arguments is passed instead of just one.