【问题标题】:Wordpress cron jobs not firingWordpress cron 作业未触发
【发布时间】:2014-11-26 15:15:34
【问题描述】:

我有一个 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 作业并继续到那个页面。

【问题讨论】:

  • 如果您删除 sendReminderEmail 函数中的所有内容并输入wp_mail( 'Your email address', 'Test', 'Testing cron job');,这样您至少会看到 cron 作业是否正常工作。如果是,则说明查询有问题。
  • @Howlin 我刚刚尝试过(确保更改函数名称以确保它在 wp_options 表中正确更新),但它似乎仍然无法正常工作。其他 wp_mail() 函数似乎会立即启动,但不是这个……我还通过在 PHPMyAdmin 中运行它来仔细检查我的 SQL,它肯定会拉出相关的行。

标签: php wordpress cron


【解决方案1】:

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

我经常在我的front-page.php 中快速测试cron 函数,方法是在顶部添加sendReminderEmails(); exit; 之类的内容,以检查该函数是否正常工作。

【讨论】:

    猜你喜欢
    • 2017-07-26
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    相关资源
    最近更新 更多