WordPress Cron Job

时间:2014-12-10 13:10:26

标签: php wordpress cron

下面是什么原因:

  

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中的教程,但我需要了解一些要点:

  1. cronStartDeactive()函数的原因是什么,当我删除它时,cron作业仍然有效,那么使用它的目的是什么
  2. add_action('wp','cronStartActive')也是如此; ,当我评论它时,cron仍然有效。

0 个答案:

没有答案