之前一直没有注意到要在执行任务计划之前先清除,以至于每隔1分钟就添加一个新的任务计划,最后内存枯竭。
add_filter( 'cron_schedules', '_cron_add_minutely' );
function _cron_add_minutely( $schedules ) {
// Adds once weekly to the existing schedules.
$schedules['minutely'] = array(
'interval' => 60,
'display' => __( 'Minutely' )
);
return $schedules;
}
add_action( 'wp', '_setup_schedule' );
function _setup_schedule() {
//wp_clear_scheduled_hook( 'itc_daily_event' );
if ( ! wp_next_scheduled( 'itc_daily_event' ) ) {
wp_schedule_event(time(), 'minutely', 'itc_minutely_event' );
}
}
add_action( '_minutely_event', '_do_minutely_event' );
function _do_minutely_event() {
error_log(date('Y-m-d H:i:s') ." check you php log file");
}
删除所有的计划人物by hook name
//Removes a cron based on a hook name
function delete_cron_hooks( $hooks = array(), $blog_id = 0 ) {
if ( is_multisite() && $blog_id != 0 ) {
switch_to_blog( $blog_id );
}
$crons = get_option( 'cron' );
if ( !$crons ) return false;
foreach ( $crons as $timestamp => $cron ) {
foreach ( $hooks as $hook ) {
if ( isset( $cron[ $hook ] ) ) {
unset( $crons[ $timestamp ] );
}
}
}
update_option( 'cron', $crons );
} //end delete_cron