首先创建一个自定义间隔,你可以找到更多关于这个here的信息。
add_filter( 'cron_schedules', 'cron_add_5min' );
function cron_add_5min( $schedules ) {
$schedules['5min'] = array(
'interval' => 5*60,
'display' => __( 'Once every five minutes' )
);
return $schedules;
}
确保您已取消计划插件注册的事件:
wp_unschedule_event( next_scheduled_event( 'w4pr_cron' ), 'w4pr_cron' );
接下来安排一个具有此重复间隔的活动在凌晨 5 点开始:
if( time() > strtotime( 'today 5:00' ) )
wp_schedule_event( strtotime( 'tomorrow 5:00' ), '5min', 'w4pr_cron' );
else
wp_schedule_event( strtotime( 'today 5:00' ), '5min', 'w4pr_cron' );
w4pr_cron 不是一个函数,而是一个附加了函数的钩子。因此,您要做的是确保如果时间戳不在您给定的时间间隔内,则在调用此挂钩时不会发生任何事情。所以在functions.php或在每次页面加载时都会看到它执行的地方,放:
add_action( 'init', function() {
$start = strtotime( 'today 5:00' );
$end = strtotime( 'today 6:30' );
if( !(time() > $start && time() < $end) ) remove_all_actions( 'w4pr_cron' );
}