每次页面加载时都会调用wp Schedule事件

时间:2018-08-07 10:49:09

标签: php wordpress plugins cron

我是wordpress的新手,我正在尝试为wordpress插件创建一个cron作业,每小时调用一次以发布帖子。

我正在计划单击按钮时的事件(我不想在激活时进行计划。)。 单击按钮成功调度(我可以使用wp_get_schedule()进行检查)

以下是我的代码

if (isset($_POST['synch_categories'])) {
            wp_clear_scheduled_hook('my_hourly_event');
            if ( !wp_next_scheduled( 'my_hourly_event' ) ) {
                wp_schedule_event( current_time( 'timestamp' ), 'sixty_seconds', 'my_hourly_event');
            }
             $schedule = wp_get_schedule( 'my_hourly_event' );
             print_r($schedule);
    }

在构造函数中,我正在这样做

    add_filter( 'cron_schedules', array($this,'add_new_intervals'));
    add_action('my_hourly_event', array($this,'do_this_hourly'));

这是我的自定义间隔函数

   function add_new_intervals( $schedules ) {
        $schedules['sixty_seconds'] = array(
            'interval' => 60,
            'display'  => esc_html__( 'Every 60 Seconds' ),
        );

        return $schedules;
    }

这是功能

public function do_this_hourly() {
 //all the functionality
}

我的cronjob链接为http://96.125.170.159/~autoblog/wp-cron.php?doing_wp_cron=1

我已经在wp-config.php中添加了此林 define('DISABLE_WP_CRON',true);

我正在关注这些链接 https://tommcfarlin.com/wordpress-cron-jobs/ https://premium.wpmudev.org/blog/schedule-functions-in-your-plugins-with-wordpress-cron/

但是问题是,每次刷新页面以及实现条件时,每次都会调用do_this_hourly()函数

if($_GET['doing_wp_cron']=='1'){
        add_filter( 'cron_schedules', array($this,'add_new_intervals'));
        add_action('my_hourly_event', array($this,'do_this_hourly'));
}

函数do_this_hourly()根本不调用。

我正在努力使它从最近两天开始工作。请帮助我。

谢谢。

0 个答案:

没有答案
相关问题