下面是什么原因:
wp_unschedule_event
和
add_action'wp'
请看一下这段代码,它是一个简单的cron作业插件,每隔1分钟将数据插入数据库
<?php
/*
Plugin Name: Cron Job
Description: My Cron Job
Version: 4.0
Author: Bassem
License: GPL
*/
?>
<?php
//Add Interval [ Minute ]
function cron_add_minute($schedules)
{
// Adds once every minute to the existing schedules.
$schedules['EveryOneMinute'] = array('interval' => 60,'display' =>( 'insert record into DB' ) );
return $schedules;
}
add_filter( 'cron_schedules', 'cron_add_minute' );
//==================================
//Create a new CronJob
function cronStartActive()
{
if( !wp_next_scheduled( 'mycron' ) )
{
wp_schedule_event(time(), 'EveryOneMinute', 'mycron' );
}
}
add_action('wp', 'cronStartActive');
//==================================
function cronStartDeactive(){
$timestamp = wp_next_scheduled ('mycron');
wp_unschedule_event ($timestamp, 'mycron');
}
register_deactivation_hook (__FILE__, 'cronStartDeactive');
//================================================================
function myWpTask()
{
global $wpdb;
$date=date('Y-m-d H:i:s');
$query="insert into _testcron(added_on) values('$date')";
$insert=$wpdb->query($query);
}
add_action ('mycron', 'myWpTask');
?>
代码工作正常,我按照this link中的教程,但我需要了解一些要点: