I am using the wp_schedule_single_event()
function to create a wp-cron job that sends an email to the specified user at the specified time.
Mostly this wp-cron job is successfully created and the users get informed in time. But sometimes it just doesn't work.
Whats especially strange is that wp_schedule_single_event()
always returns true
(which means that it was executed successfully) even when the wp-cron job isn't created (I check that with the WP Crontrol plugin).
My code (write_log: custom function to log the given strings, time: the corresponding timestamp):
write_log('User ' . get_current_user_id() . ' now tries to create the addProductsExpired cron job with timestamp: ' . time);
$success = wp_schedule_single_event(time, 'hook_addProductsExpired', array(get_current_user_id()));
if (!$success) {
write_log('The creation failed!');
}
write_log('User ' . get_current_user_id() . ' now tries to create the sendReminderMail cron job with timestamp: ' . time);
$success = wp_schedule_single_event(time - 60 * 60 * 24, 'hook_sendReminderMail', array(get_current_user_id()));
if (!$success) {
write_log('The creation failed!');
}
I should also note that I never accomplished it to reproduce the error by myself
I so far tried:
updating Wordpress
studying the logs
executing the function with accounts of users where it previously failed (it worked on my pc and also on the pc of the user in future executions)
modifying parameters in the user entry of affected users
manually executing the function with the parameters it previously failed
rewriting and optimising the whole function
None of them worked or threw an error i could debug.
I am now using actionsheduler.org as suggested by Terminator-Barbapapa in his comment to my question. So far I haven't experienced any issues.