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?
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?
Share Improve this question asked Dec 6, 2019 at 13:22 JussiJussi 234 bronze badges 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 Commented Dec 6, 2019 at 13:43
1 Answer
Reset to default 0Uh, 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.