在wordpress插件

时间:2018-04-21 10:36:01

标签: wordpress cron

我在执行此操作时遇到困难,并检查过以前的问题,但它们似乎无法正常工作。

到目前为止,我通过在wp-config.php中添加以下内容来禁用默认的wordpress cron:

define('DISABLE_WP_CRON', true);

然后我试图安排我的任务从我的插件主php文件中每隔5分钟运行一次:

function my_cron_schedules($schedules){
    if(!isset($schedules["5min"])){
        $schedules["5min"] = array(
            'interval' => 5*60,
            'display' => __('Once every 5 minutes'));
    }
    if(!isset($schedules["30min"])){
        $schedules["30min"] = array(
            'interval' => 30*60,
            'display' => __('Once every 30 minutes'));
    }
    return $schedules;
}

add_filter('cron_schedules','my_cron_schedules');

function schedule_my_cron(){
    wp_schedule_event(time(), '5min', 'fivemin_schedule_hook');
}

if(!wp_get_schedule('fivemin_schedule_hook')){
    add_action('init', 'schedule_my_cron',10);
}

function fivemin_schedule_hook() {
    get_feed();
}

所以上面似乎是在数据库中安排我的事件,但在检查cron时间表时有100个条目:

<?php print_r(get_option('cron')); ?>

我还确保用以下内容更新我的crontab:

* * * * * wget -q -O - http://wordpress.com/wp-cron.php?doing_wp_cron

但是我的任务似乎没有运行,并担心这5分钟工作的数据库中的条目数量。

每个条目如下所示:

 Array ( [1524308364] => Array ( [fivemin_schedule_hook] => Array ( [40cd750bba9870f18aada2478b24840a] => Array ( [schedule] => 5min [args] => Array ( ) [interval] => 300 ) ) ) 

我试过调试wp-cron.php,当它试图触发时回显出$ hook,当我直接访问wp-cron.php时显示我的钩子。然而实际的功能似乎并没有激发。

1 个答案:

答案 0 :(得分:1)

试试这个:

替换它:

function schedule_my_cron(){
    wp_schedule_event(time(), '5min', 'fivemin_schedule_hook');
}

if(!wp_get_schedule('fivemin_schedule_hook')){
    add_action('init', 'schedule_my_cron',10);
}

..用这个:

function schedule_my_cron(){
    // Schedules the event if it's NOT already scheduled.
    if ( ! wp_next_scheduled ( 'my_5min_event' ) ) {
        wp_schedule_event( time(), '5min', 'my_5min_event' );
    }
}

// Registers and schedules the my_5min_event cron event.
add_action( 'init', 'schedule_my_cron' );

// Runs fivemin_schedule_hook() function every 5 minutes.
add_action( 'my_5min_event', 'fivemin_schedule_hook' );
//add_action( 'my_5min_event', 'another_function_to_call' );
//add_action( 'my_5min_event', 'another_function_to_call2' );

但更恰当/首选的方法是在插件的激活功能中添加它:

wp_schedule_event( time(), '5min', 'my_5min_event' );

示例:

register_activation_hook( __FILE__, 'my_plugin_activation' );
function my_plugin_activation() {
    if ( ! wp_next_scheduled ( 'my_5min_event' ) ) {
        wp_schedule_event( time(), '5min', 'my_5min_event' );
    }
}

..将用于代替以下内容:

function schedule_my_cron(){
    // Schedules the event if it's NOT already scheduled.
    if ( ! wp_next_scheduled ( 'my_5min_event' ) ) {
        wp_schedule_event( time(), '5min', 'my_5min_event' );
    }
}

// Registers and schedules the my_5min_event cron event.
add_action( 'init', 'schedule_my_cron' );

在插件的停用功能中添加此内容

wp_clear_scheduled_hook( 'my_5min_event' );

示例:

register_deactivation_hook( __FILE__, 'my_plugin_deactivation' );
function my_plugin_deactivation() {
    wp_clear_scheduled_hook( 'my_5min_event' );
}

有关详细信息,请参阅https://codex.wordpress.org/Function_Reference/wp_schedule_event#Examples