自己寫外掛程式實現wordpress評論回複郵件通知

來源:互聯網
上載者:User

評論回複郵件通知是指:你回複了A的評論,A會收到相應的郵件通知。

建立一個外掛程式,只需要一個php檔案就可以了.

首先是add_action

add_action('comment_post', 'comment_notify');

然後實現我們的發送郵件函數。主要就是根據wordpress的API擷取相關資訊,然後調用wp_mail函數進行發送即可。

修改源郵件地址可以在header裡面修改:

$message_headers = 'From: Acm之家 <no-reply@acmerblog.com>' . "\r\n\\";

注意區分 comment 和 old_comment。後者指被回複的評論。

這裡以我的網站 ACM之家  www.acmerblog.com 為例:

<?phpadd_action('comment_post', 'comment_notify');function comment_notify($comment_reply_id){    $comment = get_comment($comment_reply_id);     if($comment->comment_parent != 0)    {        $old_comment = get_comment($comment->comment_parent);//被回複的評論        if($old_comment->user_id == 0)        {            $email = $old_comment->comment_author_email;  //A評論者的郵箱,作為目的地址            $name = $comment->comment_author;             $content = $comment->comment_content;            $post = get_post($comment->comment_post_ID);            $title = $post->post_title;            $link = get_permalink($comment->comment_post_ID);            $bname=get_option('blogname'); //擷取到網站的名稱, ACM之家。 這裡沒有用這個變數,後面直接寫死了            $blogname = wp_specialchars_decode($bname, ENT_QUOTES); $subject = sprintf('[%1$s]在[%2$s]中回複了你', $name, $title ); //郵件的主題//$subject = sprintf('[%1$s] 評論回複: "%2$s"', $blogname, $title );            $notify_message  = sprintf('你在《%s》的評論有新回複', $title ) . "\r\n";            $notify_message .= sprintf( '評論者 : %1$s ', $name ) . "\r\n";            $notify_message .= '評論內容: ' . "\r\n" . $content . "\r\n\r\n";            $notify_message .= '您可在這裡查看這篇文章的評論: ' . "\r\n";            $notify_message .= $link . "#comments\r\n\r\n";        $message_headers = 'From: Acm之家 <no-reply@acmerblog.com>' . "\r\n\\"; //header的應用可以更改郵件的源地址        $message_headers .= "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";            wp_mail( $email, $subject, $notify_message, $message_headers );        }    }}?>




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.