出于测试目的,我为cron保留较短的时间间隔,当功能正常工作时,我将其更改为所需的时间间隔。每当我将例如的时间间隔从“三天”更改为“五分钟”,或从“五分钟”更改为“十五分钟”,cron都会以较早的频率运行,而不是更新的频率。我完全不明白这一点。即使设置了新的时间间隔,cron函数也只在以前的时间间隔上运行。
这可能是什么原因,请帮我解决这个问题。
这是我的代码:
add_filter(\'cron_schedules\', \'filter_cron_schedules\');
function filter_cron_schedules($schedules) {
$schedules[\'fifteen_minutes\'] = array(
\'interval\' => 900, // seconds
\'display\' => __(\'Every 15 minutes\')
);
$schedules[\'twenty_minutes\'] = array(
\'interval\' => 1200, // seconds
\'display\' => __(\'Every 20 minutes\')
);
$schedules[\'three_days\'] = array(
\'interval\' => 259200, // seconds
\'display\' => __(\'Every 3 days\')
);
$schedules[\'five_minutes\'] = array(
\'interval\' => 300, // seconds
\'display\' => __(\'Every 5 minutes\')
);
return $schedules;
}
// Schedule the cron
add_action(\'wp\', \'bd_cron_activation\');
function bd_cron_activation() {
if (!wp_next_scheduled(\'bd_cron_cache\')) {
wp_schedule_event(time(), \'twenty_minutes\', \'bd_cron_cache\'); // hourly, daily, twicedaily
}
}
// Firing the function
add_action(\'bd_cron_cache\', \'bd_data\');
function bd_data() {
// My Logic
}
最合适的回答,由SO网友:TheDeadMedic 整理而成
每当我将例如的时间间隔从“三天”更改为“五分钟”,或从“五分钟”更改为“十五分钟”,cron都会以较早的频率运行,而不是更新的频率。
因为您仅在事件不存在时才重新安排事件:
if ( ! wp_next_scheduled( \'bd_cron_cache\' ) ) {
// schedule
}
。。。这将永远是
false
一旦您开始安排活动。相反,请对照您实际希望的值检查存储的任务计划:
if ( wp_get_schedule( \'bd_cron_cache\' ) !== \'twenty_minutes\' ) {
// Above statement will also be true if NO schedule exists, so here we check and unschedule if required
if ( $time = wp_next_schedule( \'bd_cron_cache\' ) )
wp_unschedule_event( $time, \'bd_cron_cache\' );
wp_schedule_event( \'bd_cron_cache\', \'twenty_minutes\' );
}
SO网友:immazharkhan
// Custom Cron Schedules
add_filter(\'cron_schedules\', \'filter_cron_schedules\');
function filter_cron_schedules($schedules) {
$schedules[\'two_minutes\'] = array(
\'interval\' => 120, // seconds
\'display\' => __(\'Every 2 minutes\')
);
$schedules[\'twenty_minutes\'] = array(
\'interval\' => 1200, // seconds
\'display\' => __(\'Every 20 minutes\')
);
return $schedules;
}
// Schedule the cron
add_action(\'wp\', \'bd_cron_activation\');
function bd_cron_activation() {
if ( wp_get_schedule(\'bd_cron_cache\') !== \'two_minutes\' ) {
// Above statement will also be true if NO schedule exists, so here we check and unschedule if required
if ( $time = wp_next_scheduled(\'bd_cron_cache\'))
wp_unschedule_event($time, \'bd_cron_cache\');
wp_schedule_event(time(),\'two_minutes\',\'bd_cron_cache\');
}
}
// Firing the function
add_action(\'bd_cron_cache\', \'bd_data\');
function bd_data() {
// My Logic
}