3

This question already has an answer here:

I declared an action for a single event in Wordpress which accepts an array as an argument:

$asins = array(); //I had to declare this since I'm getting a notice of undefined variable if I don't

add_action('z_purge_products_cache', array($this, 'purge_products_cache', $asins));

I also tried this one, but it doesn't perform the action either:

add_action('z_purge_products_cache', array($this, 'purge_products_cache'));

Then I schedule it:

wp_schedule_single_event(time() + 20, 'z_purge_products_cache', $asins_r);

Then here's the function that will be called once wp cron executes the action:

public function purge_products_cache($asins){
  //send email here
}

Any ideas?

marked as duplicate by fuxia Aug 14 '13 at 5:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

The parameter needs to be passed to the callback function in the wp_schedule_single_event function, not the add_action function.

Try this:

add_action('z_purge_products_cache', array($this, 'purge_products_cache'));

Then schedule the event, putting the parameter in an array:

wp_schedule_single_event(time() + 20, 'z_purge_products_cache', array($asins_r));

Your purge_products_cache function will be called with parameter $asins_r.

  • The method that I'm trying to call is inside the class so I had to use the $this keyword. This doesn't seem to work either. – soul Aug 14 '13 at 4:38
  • wp_schedule_single_event needs to send the parameters in an array. See this example from the codex. I've updated the answer, including a new add_action line. – Ben Miller - Reinstate Monica Aug 14 '13 at 5:02
-2

Passing Argument in add_action() just see the following example ,

  function email_friends( $post_ID )  
{
   $friends = 'bob@example.org, susie@example.org';
   wp_mail( $friends, "sally's blog updated", 'I just put something on my blog: http://blog.example.com' );

   return $post_ID;
}
add_action( 'publish_post', 'email_friends' );

Brief explanation about add_action() is here.

  • 2
    You didn’t pass any argument to add_action() in your example. – fuxia Aug 14 '13 at 5:24

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