當(dāng)前位置:首頁>WordPress建站>用戶交互>WordPress添加評論回復(fù)郵件提醒通知功能

WordPress添加評論回復(fù)郵件提醒通知功能

評論回復(fù)后,自動發(fā)一封郵件提醒評論人,是提高用戶體驗的一大舉措。倡萌一直都在使用Willin Kan大師的評論回復(fù)郵件提醒通知代碼,相信很多人也在使用,如果你還沒有使用,不妨試試。

根據(jù)自己的需要,選擇一種自己需要的代碼,添加在主題的 functions.php 文件的 最后一個 ?> 前面即可:

方法一:所有回復(fù)都發(fā)送郵件通知

默認(rèn)所有填寫了郵箱的評論都將發(fā)郵件提醒評論人,沒有任何勾選設(shè)置。

/* comment_mail_notify v1.0 by willin kan. (所有回復(fù)都發(fā)郵件) */
function comment_mail_notify($comment_id) {
  $comment = get_comment($comment_id);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  $spam_confirmed = $comment->comment_approved;
  if (($parent_id != '') && ($spam_confirmed != 'spam')) {
    $wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); //e-mail 發(fā)出點, no-reply 可改為可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回復(fù)';
    $message = '
    <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
      <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
      <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
       . trim(get_comment($parent_id)->comment_content) . '</p>
      <p>' . trim($comment->comment_author) . ' 給您的回復(fù):<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以點擊 查看回復(fù)完整內(nèi)容</p>
      <p>歡迎再度光臨 ' . get_option('blogname') . '</p>
      <p>(此郵件由系統(tǒng)自動發(fā)送,請勿回復(fù).)</p>
    </div>';
      $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
      $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
      wp_mail( $to, $subject, $message, $headers );
  }
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------

方法二:讓訪客自己選擇是否郵件通知

在評論框下方顯示一個勾選框,讓評論人自己決定是否接收郵件通知。

/* 開始*/
function comment_mail_notify($comment_id) {
  $admin_notify = '1'; // admin 要不要收回復(fù)通知 ( '1'=要 ; '0'=不要 )
  $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
  $comment = get_comment($comment_id);
  $comment_author_email = trim($comment->comment_author_email);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  global $wpdb;
  if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
    $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
  if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
    $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
  $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
  $spam_confirmed = $comment->comment_approved;
  if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
    $wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發(fā)出點, no-reply 可改為可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回復(fù)';
    $message = '
    <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
      <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
      <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
       . trim(get_comment($parent_id)->comment_content) . '</p>
      <p>' . trim($comment->comment_author) . ' 給您的回復(fù):<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以點擊查看回復(fù)的完整內(nèi)容</p>
      <p>還要再度光臨 ' . get_option('blogname') . '</p>
      <p>(此郵件由系統(tǒng)自動發(fā)送,請勿回復(fù).)</p>
    </div>';
         $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
         $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
         wp_mail( $to, $subject, $message, $headers );
  }
}
add_action('comment_post', 'comment_mail_notify');
 
/* 自動加勾選欄 */
function add_checkbox() {
  echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回復(fù)時郵件通知我</label>';
}
add_action('comment_form', 'add_checkbox');

方法三:讓博客管理員決定什么情況下發(fā)郵件

你可以根據(jù)自己的需求,配置下面代碼(看代碼注釋),決定什么情況才發(fā)郵件。

/* comment_mail_notify v1.0 by willin kan. (無勾選欄) */
function comment_mail_notify($comment_id) {
  $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
  $comment = get_comment($comment_id);
  $comment_author_email = trim($comment->comment_author_email);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
  $spam_confirmed = $comment->comment_approved;
  if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
    /* 上面的判斷式,決定發(fā)出郵件的必要條件:
    ($parent_id != '') && ($spam_confirmed != 'spam'): 回復(fù)的, 而且不是 spam 才可發(fā), 必需!!
    ($to != $admin_email) : 不發(fā)給 admin.
    ($comment_author_email == $admin_email) : 只有 admin 的回復(fù)才可發(fā).
    可視個人需修改上面的條件.
    */
    $wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發(fā)出點, no-reply 可改為可用的 e-mail.
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回復(fù)';
    $message = '
    <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
      <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
      <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
       . trim(get_comment($parent_id)->comment_content) . '</p>
      <p>' . trim($comment->comment_author) . ' 給您的回復(fù):<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以點擊 查看回復(fù)的完整內(nèi)容</p>
      <p>還要再度光臨 ' . get_option('blogname') . '</p>
      <p>(此郵件由系統(tǒng)自動發(fā)送,請勿回復(fù).)</p>
    </div>';
         $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
         $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
         wp_mail( $to, $subject, $message, $headers );
  }
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------

方法四:支持嵌套和@用戶方式的評論提醒

此方法轉(zhuǎn)載自zww.me,這版本的評論回復(fù)通知是支持嵌套和@用戶方式的。有什么問題,請到作者頁面反饋。

/* 郵件通知 by Qiqiboy */
 function comment_mail_notify($comment_id) {
     $comment = get_comment($comment_id);//根據(jù)id獲取這條評論相關(guān)數(shù)據(jù)
     $content=$comment->comment_content;
     //對評論內(nèi)容進行匹配
     $match_count=preg_match_all('/<a href="#comment-([0-9]+)?" rel="nofollow">/si',$content,$matchs);
     if($match_count>0){//如果匹配到了
         foreach($matchs[1] as $parent_id){//對每個子匹配都進行郵件發(fā)送操作
             SimPaled_send_email($parent_id,$comment);
         }
     }elseif($comment->comment_parent!='0'){//以防萬一,有人故意刪了@回復(fù),還可以通過查找父級評論id來確定郵件發(fā)送對象
         $parent_id=$comment->comment_parent;
         SimPaled_send_email($parent_id,$comment);
     }else return;
 }
 add_action('comment_post', 'comment_mail_notify');
 function SimPaled_send_email($parent_id,$comment){//發(fā)送郵件的函數(shù) by Qiqiboy.com
     $admin_email = get_bloginfo ('admin_email');//管理員郵箱
     $parent_comment=get_comment($parent_id);//獲取被回復(fù)人(或叫父級評論)相關(guān)信息
     $author_email=$comment->comment_author_email;//評論人郵箱
     $to = trim($parent_comment->comment_author_email);//被回復(fù)人郵箱
     $spam_confirmed = $comment->comment_approved;
     if ($spam_confirmed != 'spam' && $to != $admin_email && $to != $author_email) {
         $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發(fā)出點, no-reply 可改為可用的 e-mail.
         $subject = '您在 [' . get_option("blogname") . '] 的留言有了回應(yīng)';
         $message = '<div style="background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding:0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;">
             <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
             <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
             . trim(get_comment($parent_id)->comment_content) . '</p>
             <p>' . trim($comment->comment_author) . ' 給你的回復(fù):<br />'
             . trim($comment->comment_content) . '<br /></p>
             <p>您可以點擊 <a href="' . htmlspecialchars(get_comment_link($parent_id,array("type" => "all"))) . '">查看回復(fù)的完整內(nèi)容</a></p>
             <p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
             <p>(此郵件有系統(tǒng)自動發(fā)出, 請勿回復(fù).)</p></div>';
         $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
         $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
         wp_mail( $to, $subject, $message, $headers );
     }
 }

備注說明

發(fā)送郵件,需要主機支持 mail() 函數(shù),如果你發(fā)現(xiàn)沒辦法收到郵件,可以詢問你的主機商。由于每個人的主機環(huán)境不一樣,有些朋友在添加這個功能的時候,總是不能成功,這時候,你可以試試 SMTP 發(fā)送郵件的方式。

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號 WPDAXUE
WordPress插件用戶交互

WordPress文章評分插件:WP-PostRatings

2012-11-25 5:51:00

用戶交互

WordPress邊欄調(diào)用帶Gravatar頭像的最新評論

2012-12-25 6:01:00

34 條回復(fù) A文章作者 M管理員
  1. 戰(zhàn)無不勝

    倡萌老師還有一個問題,現(xiàn)在手機實名,不用手機,用郵箱多落后?

  2. 戰(zhàn)無不勝

    倡萌老師,你氣死我了。為什么要發(fā)郵件呢?那多麻煩呀。我在自己電腦網(wǎng)站上操作,人家留言直接留言站點上,順便不就看了嗎,發(fā)到郵箱里我也不知道,就是知道了要到別人的網(wǎng)站登陸等等多麻煩。今天我試驗了35款留言插件,全是發(fā)郵件,氣死我了,就找不到一個留言網(wǎng)站上的,留言有什么見不擔(dān)人的呢?
    老師你可得給我想點辦法

?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

濉溪县| 时尚| 樟树市| 广东省| 建瓯市| 乌鲁木齐县| 长泰县| 依安县| 周宁县| 夹江县| 江油市| 湾仔区| 临澧县| 全南县| 定远县| 齐齐哈尔市| 象州县| 苍梧县| 东源县| 田阳县| 南京市| 富源县| 澄迈县| 大城县| 汾西县| 沿河| 达拉特旗| 望都县| 新乐市| 阿图什市| 麟游县| 蕲春县| 磐安县| 邢台市| 铜川市| 德庆县| 天台县| 高要市| 仁布县| 东安县| 友谊县|