【问题标题】:Setting up a cron job corectly within wordpress plugin在 wordpress 插件中正确设置 cron 作业
【发布时间】:2018-04-22 20:53:34
【问题描述】:

我在执行此操作时遇到了困难,并检查了以前的问题,但它们似乎没有用。

到目前为止,我已经通过在我的 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 ) ) ) 

我尝试通过在尝试触发时回显 $hook 来调试 wp-cron.php,并且当我直接访问 wp-cron.php 时会显示我的钩子。然而,实际功能似乎并没有触发。

【问题讨论】:

  • 您是否检查过手动呼叫http://wordpress.com/wp-cron.php?doing_wp_cron 是否有效?
  • 是的,检查似乎没有触发我的功能
  • 您的 crontab 可以使用 */5 * * * crontab.guru/#*/5__** 每 5 分钟安排一次
  • 是的,我认为只要超过 5 分钟就可以了,因为也可以每分钟执行一次 cron

标签: wordpress cron


【解决方案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

【讨论】:

  • 我会试一试,我想我已经成功了,虽然我觉得它现在每分钟都在运行。感谢您的帮助,我对 wordpress 有点陌生,只是想了解一下。
  • 希望该代码对您有用,因为我已经对其进行了实际测试,它只创建了一个事件,并且每隔 5 分钟触发了与该事件挂钩的函数 fivemin_schedule_hook()
猜你喜欢
  • 2012-07-21
  • 2012-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多