wp_schedule_event挂钩已安排但无法正常工作

时间:2015-06-15 08:02:52

标签: php wordpress cron cron-task

我正在尝试从我正在编写的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
}

1 个答案:

答案 0 :(得分:2)

尝试将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!');
}