I created a wp cron event with a custom interval of 5 minutes, but it is only executed hourly instead of every 5 minutes. (The callback is properly executed.)
The DISABLE_WP_CRON constant is set to true, wp-cron.php is called via crontab every 5 minutes. (https://developer.wordpress.org/plugins/cron/hooking-into-the-system-task-scheduler/)
No errors in debug.log (WP_DEBUG set to true).
I created my plugin with the wordpress plugin boilderplate generator (https://wppb.me/).
My code (in class Wp_Goldprice_Activator, function activate()):
function fetch_metal_prices_recurrence( $schedules ) {
$schedules['every_five_minutes'] = array(
'display' => 'every 5 minutes',
'interval' => 300
);
return $schedules;
}
add_filter( 'cron_schedules', 'fetch_metal_prices_recurrence' );
// Schedule custom-interval Cron Job Event
if ( ! wp_next_scheduled ( GPR_PLUGIN_NAME . '_fetch_metal_prices' )) {
wp_schedule_event( current_time( 'timestamp' ), 'every_five_minutes', GPR_PLUGIN_NAME . '_fetch_metal_prices' );
}
// Schedule hourly Cron Job Event
if ( ! wp_next_scheduled ( GPR_PLUGIN_NAME . '_fetch_metal_prices_reschedule' )) {
wp_schedule_event( current_time( 'timestamp' ), 'hourly', GPR_PLUGIN_NAME . '_fetch_metal_prices_reschedule' );
}
}
I am rescheduling the 5 minute event every hour, because it disappears after first automatic execution (not on manual execution, though!). The hourly event doesn’t disappear (on automatic execution). I couldn’t find a reason or solution for that, either. This might be involved in the problem, but still, wp_next_scheduled() immediately returns a time which is 1 hour in the future, not 5.
I am aware, this question has been asked before, but no working solution has been proposed (and posted years ago):
- wp_schedule_event / cron_schedules – custom recurrence time not working in Plugin
- Custom interval is not working
- custom cron interval is not working
Any thoughts on that would be greatly appreciated, I am out of ideas.