首页 文章

Wordpress cron工作没有解雇

提问于
浏览
1

我有一个wordpress网站,允许用户设置某一天的提醒,它将其存储在一个数据库中,可以在用户登录时显示,或向他们发送电子邮件 .

我按照http://codex.wordpress.org/Function_Reference/wp_cron中给出的示例代码进行了操作,并将下面的代码放入我编写的插件的主.php文件中,该文件在所有其他方面都完美运行 .

if ( ! wp_next_scheduled( 'checkReminderEmails' ) ) {
    wp_schedule_event( 1411693200, 'daily', 'checkReminderEmails' );
}                    //1411693200 is the unix timestamp for 1am a couple of days ago

add_action( 'checkReminderEmails', 'sendReminderEmails' );

function sendReminderEmails() 
{
    $table_name = $wpdb->prefix."reminders";
    $query = "SELECT * FROM $table_name WHERE sendReminder = 1 and reminderDate = CURRENT_DATE( )";

    $appointments = $wpdb->get_results( $query); 
        foreach ( $appointments as $info ) 
        {
            $message = "Hello here is your reminder.";
            $toAddress = $info->userEmail;
            $subject = "This is your reminder.";
            wp_mail( $toAddress, $subject, $message);
        }
} // end sendReminderEmails()

我检查了PHP数据库中的wp_options表,可以在那里看到以下代码

{s:18:"sendReminderEmails";a:1:{s:32:"40cd750bba9870f18aada2478b24840a";a:3:{s:8:"schedule";s:5:"daily";s:4:"args";a:0:{}s:8:"interval";i:86400;}}}i:1411693200;a:1:

我可以使用wp_mail()从网站上收到其他电子邮件,所以我知道服务器支持该功能,我知道wp_cron作业在时间过后访问网站之前不会被解雇,但我一直在无法正确启动此cron作业 . 我错过了一些明显的东西吗

编辑:对于任何想知道的人,我使用了wp-cron-dashboard插件(https://wordpress.org/plugins/wp-cron-dashboard/)尽管警告说它在2年内没有更新,以检查我的cron作业是否安排得当 .

我还必须添加全局$ wpdb;在我的函数的顶部,它未能触发的原因是因为没有声明我无法使用get_results()函数 .

我还发现,如果你去wp-cron.php,你将手动强制任何预定的cron作业被触发,并且所有输出都将显示在那里,因此可以通过添加echo语句并转到该页面来调试cron作业 .

1 回答

  • 0

    我知道这是旧的,但我立即看到的一件事是你需要 global $wpdb; 作为 sendReminderEmails() 函数的第一行,否则它会爆炸 .

    我经常在我的 front-page.php 中快速测试cron函数,在顶部添加类似 sendReminderEmails(); exit; 的内容,就像该函数完全正常的健全性检查一样 .

相关问题