Nic*_*ick 5 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
}
Run Code Online (Sandbox Code Playgroud)
尝试移出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!');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3137 次 |
| 最近记录: |