簡體   English   中英

WordPress的Cron工作不解雇

[英]Wordpress cron jobs not firing

我有一個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作業。 。

我知道這已經很老了,但是我馬上看到的一件事是您需要global $wpdb; 作為sendReminderEmails()函數的第一行,否則它將被炸毀。

我經常通過添加諸如sendReminderEmails(); exit;類的東西來快速測試front-page.php的cron函數sendReminderEmails(); exit; sendReminderEmails(); exit; 就像檢查功能是否完全正常一樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM