Wordpress cronjob每3分钟一次

时间:2016-09-12 03:31:06

标签: php wordpress cron

在这个主题上有很多关于stackoverflow的帖子但是(我不知道为什么)没有什么对我有用。

我有什么

function isa_add_every_three_minutes( $schedules ) {

    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );

    return $schedules;
}
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );


function every_three_minutes_event_func() 
{
    // do something
}

wp_schedule_event( time(), 'every_three_minutes', 'every_three_minutes_event_func' );

任何想法我做错了什么?

3 个答案:

答案 0 :(得分:20)

您需要使用add_action()将您的功能挂钩到预定的活动。

add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );

这是完整的代码。     

// Add a new interval of 180 seconds
// See http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

// Schedule an action if it's not already scheduled
if ( ! wp_next_scheduled( 'isa_add_every_three_minutes' ) ) {
    wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes' );
}

// Hook into that action that'll fire every three minutes
add_action( 'isa_add_every_three_minutes', 'every_three_minutes_event_func' );
function every_three_minutes_event_func() {
    // do something
}
?>

答案 1 :(得分:5)

答案 2 :(得分:0)

这个代码所在的第一个重要部分是你的functions.php中的主题吗?或者它是您开发的自定义插件?

根据我的经验,通过自定义插件更容易调试和激活cron,使用激活挂钩来激活和停用事件。我之前很难通过函数php激活cron事件,我更喜欢通过自定义插件激活这些事件。

我会从这样的插件结构开始:

  • /我的-时钟守护插件
  • /my-cron-plugin/index.php
  • /my-cron-plugin/my-cron-plugin.php

index.php内容:

<?php // silence is golden

my-cron-plugin.php内容:

<?php

/**
 * Plugin name: My Custom Cron Plugin
 * Description: Simple WP cron plugin boilerplate.
 * Author: Your name
 * Version: 0.1
 */

// Security reasons...
if( !function_exists( 'add_action' ) ){
    die('...');
}

// The activation hook
function isa_activation(){
    if( !wp_next_scheduled( 'isa_add_every_three_minutes_event' ) ){
        wp_schedule_event( time(), 'every_three_minutes', 'isa_add_every_three_minutes_event' );
    }
}

register_activation_hook(   __FILE__, 'isa_activation' );

// The deactivation hook
function isa_deactivation(){
    if( wp_next_scheduled( 'isa_add_every_three_minutes_event' ) ){
        wp_clear_scheduled_hook( 'isa_add_every_three_minutes_event' );
    }
}

register_deactivation_hook( __FILE__, 'isa_deactivation' );


// The schedule filter hook
function isa_add_every_three_minutes( $schedules ) {
    $schedules['every_three_minutes'] = array(
            'interval'  => 180,
            'display'   => __( 'Every 3 Minutes', 'textdomain' )
    );
    return $schedules;
}

add_filter( 'cron_schedules', 'isa_add_every_three_minutes' );


// The WP Cron event callback function
function isa_every_three_minutes_event_func() {
    // do something
}

add_action( 'isa_add_every_three_minutes_event', 'isa_every_three_minutes_event_func' );

设置此插件后,应在激活插件时激活该事件。要测试它的工作是否正在使用此插件:https://wordpress.org/plugins/wp-crontrol/

了解WP cron如何工作的另一个好资源是:https://developer.wordpress.org/plugins/cron/