插件中的wp_schedule_event()没有调度cron事件

时间:2017-11-12 13:48:57

标签: php wordpress plugins cron

我正在创建一个WordPress插件,当插件被激活时,我需要安排一个cron作业,每5分钟运行一次。

这是我的代码;

// Register plugin activation hook
function my_plugin_activate() {
    if( !wp_next_scheduled( 'my_function_hook' ) ) {  
       wp_schedule_event( time(), '5', 'my_function_hook' );  
    }
}
register_activation_hook( __FILE__, 'my_plugin_activate' );

// Register plugin deactivation hook
function my_plugin_deactivate(){
    wp_clear_scheduled_hook('my_function_hook');
}
register_deactivation_hook(__FILE__,'my_plugin_deactivate');

// Function I want to run when cron event runs
function my_function(){
    //Function code
}
add_action( 'my_function_hook', 'my_function');

当我使用此插件https://wordpress.org/plugins/wp-crontrol/检查cron事件时,没有添加任何内容,我期待添加一个以5分钟为间隔运行'my_function'的cron事件,我没有错误

3 个答案:

答案 0 :(得分:5)

请参阅:wp_schedule_event()

  

重复的有效值是每小时,每天和每日两次。   这些可以使用'cron_schedules'过滤器进行扩展   wp_get_schedules()。

因此,您只需添加一个每5分钟运行一次的自定义计划。

<?php // Requires PHP 5.4+.

add_filter( 'cron_schedules', function ( $schedules ) {
    $schedules['every-5-minutes'] = array(
        'interval' => 5 * MINUTE_IN_SECONDS,
        'display'  => __( 'Every 5 minutes' )
    );
    return $schedules;
} );

if( ! wp_next_scheduled( 'my_function_hook' ) ) {  
    wp_schedule_event( time(), 'every-5-minutes', 'my_function_hook' );  
}

答案 1 :(得分:2)

当有人访问你的网站时,WP Cron会运行。 因此,如果没有人访问,cron永远不会运行。

现在有两种解决方案:

  1. 禁用WP Cron,使用真正的cron作业并进行自定义。
  2. https://support.hostgator.com/articles/specialized-help/technical/wordpress/how-to-replace-wordpress-cron-with-a-real-cron-job

    1. wp_schedule_event()中使用自定义时间间隔:

      function myprefix_custom_cron_schedule( $schedules ) {
          $schedules['every_six_hours'] = array(
              'interval' => 21600, // Every 6 hours
              'display'  => __( 'Every 6 hours' ),
          );
          return $schedules;
      }
      add_filter( 'cron_schedules', 'myprefix_custom_cron_schedule' );
      
      //Schedule an action if it's not already scheduled
      if ( ! wp_next_scheduled( 'myprefix_cron_hook' ) ) {
          wp_schedule_event( time(), 'every_six_hours', 'myprefix_cron_hook' );
      }
      
      ///Hook into that action that'll fire every six hours
       add_action( 'myprefix_cron_hook', 'myprefix_cron_function' );
      
      //create your function, that runs on cron
      function myprefix_cron_function() {
          //your function...
      }
      
    2. 你可以看到这些东西

      http://www.nextscripts.com/tutorials/wp-cron-scheduling-tasks-in-wordpress/

      http://www.iceablethemes.com/optimize-wordpress-replace-wp_cron-real-cron-job/

      http://www.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/

      自定义Wp cron

      http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules

      http://www.smashingmagazine.com/2013/10/16/schedule-events-using-wordpress-cron/

      http://www.viper007bond.com/2011/12/14/how-to-create-custom-wordpress-cron-intervals/

      http://www.sitepoint.com/mastering-wordpress-cron/

      https://tommcfarlin.com/wordpress-cron-jobs/

      http://www.paulund.co.uk/create-cron-jobs-in-wordpress

      cron linux

      http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

      http://www.thesitewizard.com/general/set-cron-job.shtml

      http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

      google search

答案 2 :(得分:0)

As @dingo_d commented, WordPress Cron doesn't work like server Cron. the WordPress Cron runs on page load. It checks the database for a scheduled event and if an event is scheduled it runs the task. So if nobody visits the website in a 5 minute time period no job will be run in that period. When someone does visit the website, the page load process runs and the scheduled event takes place.

It was setup like this so WordPress would work without needing any particular Cron functionality on the server.

To circumvent this you can use a service that automatically visit your website or you could setup a Cron script on your server to automatically load the page.

Assuming a linux server you ssh into the terminal then write crontab -e and hit enter. You'll enter the cron file for setting up cron jobs. Add to the file the following line:

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

substituting http://yourdomain.com for your actual website. This ensures your website is visited every 5 minutes.

I grabbed the information on how to do this from https://tommcfarlin.com/wordpress-cron-jobs/的值,以便归功于他,并且该链接还有更多WordPress cron信息。