【问题标题】:wp_schedule_event hook scheduled but not workingwp_schedule_event 挂钩已安排但不工作
【发布时间】:2015-08-30 15:37:15
【问题描述】:

我正在尝试从我正在编写的 WordPress 插件中触发 cron 作业(它将每天获取所有新产品并将它们导出到 CSV)所以问题是当我将此代码放入 functions.php 时一切都很好,代码是有效的,但是从插件文件夹中它被安排了,我可以看到它(使用 Cron View 插件)但没有执行..我发现了另一个相同的问题,但没有答案.. 似乎它并没有真正被触发或有什么东西阻止了它.. 看看我的代码..

function csv_init(){
add_action('my_hourly_event', 'Download_CSV_with_args');
}

function starthere(){
// some code here
    $file = $_SERVER['DOCUMENT_ROOT'].'/wp-content/csv_settings.php';
                            $content = serialize($args);
                            file_put_contents($file, $content);

                        wp_schedule_event( current_time( 'timestamp' ), 'hourly', 'my_hourly_event');

                            $schedule = wp_get_schedule( 'my_hourly_event' );
                            echo  wp_next_scheduled( 'my_hourly_event' ).'<br>';
                        if ($schedule){
                            echo '<h3>The "'.$schedule.'" Cron Job is running..</h3>';
                         }else {
                            echo '<h3>There are no Cron Jobs that running..</h3>';
                         }

}

function Download_CSV_with_args() {
        //execution of my code
}

【问题讨论】:

    标签: php wordpress cron cron-task


    【解决方案1】:

    尝试将add_action 移出函数:

    function starthere(){
      if (!wp_next_scheduled('my_hourly_event')) {
        wp_schedule_event( time(), 'hourly', 'my_hourly_event' );
      }
    }
    
    add_action( 'my_hourly_event', 'Download_CSV_with_args' );
    
    function Download_CSV_with_args() {
      wp_mail('you@yoursite.com', 'Automatic email', 'Cron works!');
    }
    

    【讨论】:

    • 如果对您有帮助,请不要忘记将答案标记为最佳。 :)
    猜你喜欢
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 2015-06-12
    • 2018-07-19
    相关资源
    最近更新 更多