【问题标题】:Wordpress cronjob every 3 minutesWordpress cronjob 每 3 分钟
【发布时间】:2017-01-19 11:00:16
【问题描述】:

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' );

任何想法我做错了什么?

【问题讨论】:

    标签: php wordpress cron


    【解决方案1】:

    首先,这段代码在哪里很重要,它是否在您的主题的functions.php中?还是您正在开发的自定义插件?

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

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

    • /my-cron-plugin
    • /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/

    【讨论】:

      【解决方案2】:

      您需要使用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
      }
      ?>
      

      【讨论】:

        【解决方案3】:

        【讨论】:

          猜你喜欢
          • 2011-06-21
          • 2021-04-05
          • 2012-08-26
          • 2023-03-16
          • 1970-01-01
          • 1970-01-01
          • 2018-05-12
          • 2018-05-28
          • 2014-11-02
          相关资源
          最近更新 更多