WordPress添加迴響回復郵件提醒通知功能

本文目錄1方法一:所有回復都發送郵件通知2方法二:讓訪客自己選擇是否郵件通知3方法三:讓部落格管理員決定什麼情況下發郵件4方法四:支援嵌套和@用戶方式的迴響提醒5備註說明

迴響回復後,自動發一封郵件提醒迴響人,是提高用戶體驗的一大舉措。一直都在使用Willin Kan大師的迴響回復郵件提醒通知代碼,相信很多人也在使用,如果你還沒有使用,不妨試試。

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

方法一:所有回復都發送郵件通知

默認所有填寫瞭郵箱的迴響都將發郵件提醒迴響人,沒有任何勾選設置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* comment_mail_notify v1.0 by willin kan. (所有回復都發郵件) */
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 發出點, no-reply 可改為可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有瞭回復';
    $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) . ' 給您的回復:<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以點擊 查看回復完整內容</p>
      <p>歡迎再度光臨 ' . get_option('blogname') . '</p>
      <p>(此郵件由系統自動發送,請勿回復.)</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 ----------------------------------------

/* comment_mail_notify v1.0 by willin kan. (所有回復都發郵件) */
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 發出點, no-reply 可改為可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = ‘您在 [‘ . get_option("blogname") . ‘] 的留言有瞭回復’;
$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) . ‘ 給您的回復:<br />’
. trim($comment->comment_content) . ‘<br /></p>
<p>您可以點擊 查看回復完整內容</p>
<p>歡迎再度光臨 ‘ . get_option(‘blogname’) . ‘</p>
<p>(此郵件由系統自動發送,請勿回復.)</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 —————————————-

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* 開始*/
function comment_mail_notify($comment_id) {
  $admin_notify = '1'; // admin 要不要收回復通知 ( '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 發出點, no-reply 可改為可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有瞭回復';
    $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) . ' 給您的回復:<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以點擊查看回復的完整內容</p>
      <p>還要再度光臨 ' . get_option('blogname') . '</p>
      <p>(此郵件由系統自動發送,請勿回復.)</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">有人回復時郵件通知我</label>';
}
add_action('comment_form', 'add_checkbox');

/* 開始*/
function comment_mail_notify($comment_id) {
$admin_notify = ‘1’; // admin 要不要收回復通知 ( ‘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 發出點, no-reply 可改為可用的 e-mail.
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = ‘您在 [‘ . get_option("blogname") . ‘] 的留言有瞭回復’;
$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) . ‘ 給您的回復:<br />’
. trim($comment->comment_content) . ‘<br /></p>
<p>您可以點擊查看回復的完整內容</p>
<p>還要再度光臨 ‘ . get_option(‘blogname’) . ‘</p>
<p>(此郵件由系統自動發送,請勿回復.)</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">有人回復時郵件通知我</label>’;
}
add_action(‘comment_form’, ‘add_checkbox’);

方法三:讓部落格管理員決定什麼情況下發郵件

你可以根據自己的需求,配置下面代碼(看代碼註釋),決定什麼情況才發郵件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* 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)) {
    /* 上面的判斷式,決定發出郵件的必要條件:
    ($parent_id != '') && ($spam_confirmed != 'spam'): 回復的, 而且不是 spam 才可發, 必需!!
    ($to != $admin_email) : 不發給 admin.
    ($comment_author_email == $admin_email) : 隻有 admin 的回復才可發.
    可視個人需修改上面的條件.
    */
    $wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
    $subject = '您在 [' . get_option("blogname") . '] 的留言有瞭回復';
    $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) . ' 給您的回復:<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以點擊 查看回復的完整內容</p>
      <p>還要再度光臨 ' . get_option('blogname') . '</p>
      <p>(此郵件由系統自動發送,請勿回復.)</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 ----------------------------------------

/* 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)) {
/* 上面的判斷式,決定發出郵件的必要條件:
($parent_id != ”) && ($spam_confirmed != ‘spam’): 回復的, 而且不是 spam 才可發, 必需!!
($to != $admin_email) : 不發給 admin.
($comment_author_email == $admin_email) : 隻有 admin 的回復才可發.
可視個人需修改上面的條件.
*/
$wp_email = ‘no-reply@’ . preg_replace(‘#^www.#’, ”, strtolower($_SERVER[‘SERVER_NAME’])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
$subject = ‘您在 [‘ . get_option("blogname") . ‘] 的留言有瞭回復’;
$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) . ‘ 給您的回復:<br />’
. trim($comment->comment_content) . ‘<br /></p>
<p>您可以點擊 查看回復的完整內容</p>
<p>還要再度光臨 ‘ . get_option(‘blogname’) . ‘</p>
<p>(此郵件由系統自動發送,請勿回復.)</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 —————————————-

方法四:支援嵌套和@用戶方式的迴響提醒

此方法轉載自zww.me,這版本的迴響回復通知是支援嵌套和@用戶方式的。有什麼問題,請到作者頁面反饋。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* 郵件通知 by Qiqiboy */
 function comment_mail_notify($comment_id) {
     $comment = get_comment($comment_id);//根據id獲取這條迴響相關數據
     $content=$comment->comment_content;
     //對迴響內容進行匹配
     $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){//對每個子匹配都進行郵件發送操作
             SimPaled_send_email($parent_id,$comment);
         }
     }elseif($comment->comment_parent!='0'){//以防萬一,有人故意刪瞭@回復,還可以通過查找父級迴響id來確定郵件發送對象
         $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){//發送郵件的函數 by Qiqiboy.com
     $admin_email = get_bloginfo ('admin_email');//管理員郵箱
     $parent_comment=get_comment($parent_id);//獲取被回復人(或叫父級迴響)相關信息
     $author_email=$comment->comment_author_email;//迴響人郵箱
     $to = trim($parent_comment->comment_author_email);//被回復人郵箱
     $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 發出點, no-reply 可改為可用的 e-mail.
         $subject = '您在 [' . get_option("blogname") . '] 的留言有瞭回應';
         $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) . ' 給你的回復:<br />'
             . trim($comment->comment_content) . '<br /></p>
             <p>您可以點擊 <a href="' . htmlspecialchars(get_comment_link($parent_id,array("type" => "all"))) . '">查看回復的完整內容</a></p>
             <p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
             <p>(此郵件有系統自動發出, 請勿回復.)</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 );
     }
 }

/* 郵件通知 by Qiqiboy */
function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);//根據id獲取這條迴響相關數據
$content=$comment->comment_content;
//對迴響內容進行匹配
$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){//對每個子匹配都進行郵件發送操作
SimPaled_send_email($parent_id,$comment);
}
}elseif($comment->comment_parent!=’0′){//以防萬一,有人故意刪瞭@回復,還可以通過查找父級迴響id來確定郵件發送對象
$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){//發送郵件的函數 by Qiqiboy.com
$admin_email = get_bloginfo (‘admin_email’);//管理員郵箱
$parent_comment=get_comment($parent_id);//獲取被回復人(或叫父級迴響)相關信息
$author_email=$comment->comment_author_email;//迴響人郵箱
$to = trim($parent_comment->comment_author_email);//被回復人郵箱
$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 發出點, no-reply 可改為可用的 e-mail.
$subject = ‘您在 [‘ . get_option("blogname") . ‘] 的留言有瞭回應’;
$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) . ‘ 給你的回復:<br />’
. trim($comment->comment_content) . ‘<br /></p>
<p>您可以點擊 <a href="’ . htmlspecialchars(get_comment_link($parent_id,array("type" => "all"))) . ‘">查看回復的完整內容</a></p>
<p>歡迎再度光臨 <a href="’ . get_option(‘home’) . ‘">’ . get_option(‘blogname’) . ‘</a></p>
<p>(此郵件有系統自動發出, 請勿回復.)</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 );
}
}

備註說明

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

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *