I have declared a scheduled event in a plugin like this :
function shedule_email_alerts() {
if ( !wp_next_scheduled( 'send_email_alerts_hook' ) ) {
wp_schedule_event(time(), 'hourly', 'send_email_alerts_hook');
}
}
Then i wanted to change the frequency to ‘daily’, by doing so, replacing the original function with :
function shedule_email_alerts() {
if ( !wp_next_scheduled( 'send_email_alerts_hook' ) ) {
wp_schedule_event(time(), 'daily', 'send_email_alerts_hook');
}
}
But it seems that my event is still fired every hour. I use the Core control plugin to monitor CRON tasks and it still shows as ‘once per hour’
It looks for me that you are adding this event only when there is no such event ‘send_email_alerts_hook’ scheduled yet. Try something like this and let me know if it workded.
The thing is that you will “rewrite” this event all the time, so it would be good to deactivate this function when it run first time.
The best solution would be to check how those jobs are gathered and check if this event is already added. If so then check if recurrence is different – if so reschedule. You can find this function in wp-includes/cron.php line ~63
Good luck!
Ok, I could solve the problem by using
wp_clear_scheduled_hook()
I commented out my schedule declaration and added
wp_clear_scheduled_hook('send_email_alerts_hook')
at the end. Then deactivate – reactivate my plugin, which removed my scheduled hook. Then removedwp_clear_scheduled_hook()
and uncommented my code, now the schedule was set properly.Try debuggin with this Plugin i wrote: Link to Github/Gist. It’s a little hack-ish, but it serves its purpose.
Note that the
wp_footer()
hook must be present in your theme. Else you’ll have to hook it somewhere else.