0

I have a simple cron job set up in wordpress:

add_action( 'event_hook', 'do_this_function');
wp_schedule_single_event( time() + 300, 'event_hook', array('testvariable') );

I use the function do_this_function() to send an email to a user two weeks after they have done a thing.

However, I realize now that 'event_hook' can only be in the cron job queue ONCE at the same time! I need multiple two week count downs to be running at the same time. I am a bit lost on how this can be done.

What is the workaround to this limitation? The only thing that comes to my mind is:

1 Schedule the "event_hook" to run once per day or so.

2 Call a function that will look at the arguments of the "event_hook" cron job. I would put user emails, time when email must be sent and other info in a multidimensional array

3 Iterate the multidimensional array, send email if two weeks have passed, then delete same information from the cron job array

4 Use a separate function for adding the new information from new users to the cron job array when users trigger need for email in two weeks.

Would this work?

1
  • After some reserach I'm a little desperate. Seems that cron jobs are not meant to be fiddled with after they have been created? There are functions like wp_get_scheduled_event, but that function REQUIRES that I know the exact arguments of the job I want to retrieve. There are no functions to modify the arguments of a cron job.
    – Jussi
    Dec 6, 2019 at 13:43

1 Answer 1

0

Uh, I'm really developing a habit of answering my own questions.

Turns out, you CAN queue multiple instaces for the same hook in WP-cron. The data that WP cron uses to separate the instances is the ARGUMENTS. They cannot be the same, or WP refuses to create the new cron job.

1
  • I think I have the same problem. I am creating a plugin where the user can create several scheduled campaigns, but I don't understand how to make X campaign have a different schedule and all in a single wp cron hook. I have seen a plugin that with only 1 hook schedules different times but I don't know how to do it correctly. Any guide? Aug 13 at 19:09

Your Answer

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

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