我有以下创建CSV然后发送电子邮件的代码:
function generate_and_send_csv()
{
// Removed CSV code for ease of reading
wp_mail(\'email@address.com\', \'New Data Files\', \'You have received new data files.\', $headers, $filepath);
}
//Create the cron event
if (!wp_next_scheduled(\'generate_send_csv\')) {
wp_schedule_event(\'1488934800\', \'daily\', \'generate_send_csv\');
}
// Run it
add_action(\'generate_send_csv\', \'generate_and_send_csv\');
然后,我在cPanel中设置了以下cron:
php /home/user/public_html/wp-cron.php
通过chrispage1插件安装Cronjob调度程序后,我可以看到该插件正确地将cron作业调度为每天凌晨1点。这很好,但出于某种原因,我收到了两封电子邮件。
我知道这不可能是我的代码中的任何内容,因为当我单击Cron调度器插件中的“run”选项时,我会收到一封电子邮件。WordPress运行重复的cron作业是否存在任何已知问题?
SO网友:Liam McArthur
我尝试了WP Crontrol,结果发现有两个cron作业是为generate_send_csv
. 为了解决这个问题,我在激活挂钩和停用挂钩中创建了cron,如下所示:
register_activation_hook( __FILE__, \'my_activation\' );
add_action( \'generate_send_csv\', \'generate_and_send_csv\' );
function my_activation() {
wp_schedule_event(\'1488979800\', \'daily\', \'generate_send_csv\');
}
register_deactivation_hook( __FILE__, \'my_deactivation\' );
function my_deactivation() {
wp_clear_scheduled_hook( \'generate_send_csv\' );
}