上次我們講瞭如何給 WordPress 投稿功能添加富文本編輯器,這次我們來講講如何實現:投稿者的投稿審核通過併發布後,自動給投稿者發一封郵件進行提醒。

一、添加一個存儲投稿者郵箱的自定義欄目

打開 WordPress 添加投稿功能,下面我們將對這篇文章中的代碼進行修改。在第二段代碼第 78 行插入以下代碼:

// 其中 ludou_tougao_email 是自定義欄目的名稱
add_post_meta($status, 'ludou_tougao_email', $email, TRUE);

二、添加提醒功能 php 代碼

在主題目錄下的 functions.php 添加以下 php 代碼 (將以下代碼中的露兜博客名稱和 URL 改成你自己的):

function tougao_notify($mypost) {
    $email = get_post_meta($mypost->ID, "ludou_tougao_email", true);

    if( !empty($email) ) {
        // 以下是郵件標題
        $subject = '您在露兜博客的投稿已發佈';
        // 以下是郵件內容
        $message = '
        <p><strong> 露兜博客</strong> 提醒您: 您投遞的文章 <strong>' . $mypost->post_title . '</strong> 已發佈</p>
   
        <p> 您可以點擊以下鏈接查看具體內容:<br />
        <a href="'%20.%20get_permalink(%20$mypost->ID%20)%20.%20'"> 點此查看完整內容</a></p>
        <p>===================================================================</p>
        <p><strong> 感謝您對 <a href="http://www.ludou.org" target="_blank"> 露兜博客</a> 的關注和支持</strong></p>
        <p><strong> 該信件由系統自動發出, 請勿回覆, 謝謝.</strong></p>';
       
        add_filter('wp_mail_content_type',create_function('', 'return "text/html";'));
        @wp_mail( $email, $subject, $message );
    }
}

// 當投稿的文章從草稿狀態變更到已發佈時,給投稿者發提醒郵件
add_action('draft_to_publish', 'tougao_notify', 6);

以上功能需要你的服務器支持 mail 函數。