【问题标题】:The "next run" in wp crontrol plug in is not workingwp crontrol 插件中的“下一次运行”不起作用
【发布时间】:2018-02-12 21:35:20
【问题描述】:

我想创建一个函数,该函数每 3 分钟运行一次。

我使用插件 wp-crontrol 来创建一个 cron 作业事件。

我在google和stackoverflow中搜索,function.php中的代码是

// 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() {
    // The code run update in every 3 minutes
    $update= get_field('ls_home_number_candidates');
    $updatePlusOne= $update++;
    update_post_meta(56, 'ls_home_number_candidates', $updatePlusOne);
}
?>

然后在我的 WP-Cron 事件中出现名为 isa_add_every_three_minutes

的新事件

但是事件没有运行。如果我按“立即运行”,则会出现类似“已成功执行 cron 事件 isa_add_every_three_minutes”的通知。

但这不起作用,Next Run 字段总是“现在”

请帮忙。谢谢

【问题讨论】:

    标签: php wordpress cron


    【解决方案1】:

    您代码中的函数isa_add_every_three_minutes 只是为了获取您设置的自定义计划,它与cron 名称无关。您的add_action 应该有 cron 名称,以及您要执行的 cron 函数,然后您需要调用它,所以类似于:

    // Schedule an action if it's not already scheduled
    if ( ! wp_next_scheduled( 'my_cron' ) ) {
        wp_schedule_event( time(), 'every_three_minutes', 'my_cron' );
    }
    
    // Hook into that action that'll fire every three minutes
    add_action( 'my_cron', 'every_three_minutes_event_func' );
    

    表面上看起来一切都很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-10
      • 2015-07-01
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多