Comment Mail Notify

這是由《Ajax comments 回應郵件通知》所改進的評論回應郵件通知 (Comment Mail Notify).
主要改進兩個項目:
1. 所有內置嵌套模板都適用, 不只是用於 Ajax comments.
2. 管理者在後台回覆也可自動發郵件.

安裝步驟:
1. 如果你用了上一版的 "Ajax comments 回應郵件通知", 請先在 comments-ajax.php 刪除上一版的代碼.
2. 在下面三種方式, 選擇你想用的代碼, copy 到 functions.php 的 <?php ..... ?> 區域內.

《評論回應郵件通知》的三種代碼: (按下面的標題可直接滾下去)
一、有勾選欄, 由訪客決定是否要回應郵件通知
二、無勾選欄, 由管理者決定在什麼條件下發郵件
三、所有回覆都發郵件

必須注意的是: 你的服務器一定要有 mail() 功能. 測試方式: 在登入頁故意按下 '忘記密碼', 收到郵件就有 mail() 功能; 沒收到郵件的可以下課了.
 

一、有勾選欄, 由訪客決定是否要回應郵件通知:

(會在模板自動加勾選欄, 如果不想自動加, 可把後面一小段刪除.)

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
42
43
44
/* comment_mail_notify v1.0 by willin kan. (有勾選欄, 由訪客決定) */
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>您可以點擊 <a href="'
. htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</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 );
    //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  }
}
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');

// -- 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
/* 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>您可以點擊 <a href="'
. htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</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 );
    //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  }
}
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
/* 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>您可以點擊 <a href="'
. htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</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 );
    //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  }
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------

其實以上三段代碼都是同一個程式, 內容幾乎完全相同, 只是怕有人不會改, 出錯時又來找我發問, 所以放了三種寫法供各位直接 copy.
感謝 萬戈童鞋 幫忙測試, 我們試用了幾天還沒出問題, 本想試用兩個星期再公佈, 算一算時間剛好在過年後, 到時候我可能還在休假, 還是現在先發出來當作是送給各位的過年禮物吧! 新年快樂!
 
後記: 忘了加上 spam 檢查, 幸虧萬戈童鞋及時提醒, 謝謝萬戈! (2/10 22:24 已更新)
 
◎2010/4/2 又發現這程式在評論分頁的 get_comment_link() 有個 bug.
這 bug 對使用 comments 和 trackbacks/pingbacks 分離的情況才會出現, 沒分離的是沒影響.
當直接叫用 get_comment_link() 因為沒經過 wp_list_comments('type=comment') 函數, 它會以所有的評論作為分頁對象. 所以 trackbacks/pingbacks 數量多的時候會讓 cpage 多算了, 本來是 cpage=7 會成了 cpage=8, 所以點擊郵件裏的 "查看回應完整內容" 會找不到正確評論頁面.

--- 修正方式 ----
將以上的:

1
get_comment_link($parent_id)

改成:

1
get_comment_link($parent_id, array('type' => 'comment'))

加入的參數是讓它選取 comment 數量計算就好.
評論式樣有使用 comments 和 trackbacks/pingbacks 分離的童鞋, 請進行修改. 沒用到的就不能改.
 

238 篇回應 (訪客:128 篇, 博主:83 篇, 引用:27 篇)

  1. iPhone ~

    感谢分享 刚刚试了试 成功了

  2. 忘了用了那个版的了!换吧,保险些~

  3. 大师,如何定位它的位置呢?比如像你一样把它定位在发表评论一侧?

  4. ZDAvril ~

    copy 到 functions.php 的 区域内后,出错了。晕啊。

  5. 我添加之后修改就显示错误了?跑到404页面了~!!

  6. 找到了,原来被翻译了模版函数了~找了半天~

  7. 我发现我的主题没有 functions.php
    这要怎么办啊?

  8. 介个要研究研究

  9. 请问:“一、有勾選欄, 由訪客決定是否要回應郵件通知:”这个,是不是默认状态是已经打勾的?

    谢谢!

  10. 你好大师,我发现我采用你的第一段代码的时候,在评论框中添加的“有人回覆時郵件通知我”成了乱码,我将网页调成gb2312,这个就正常了。然后我采用了第三段代码,结果成功了,不过,文件中的那些字全部都不见了。只剩下
    ============我是可爱的分割线===========
    小七, !

    梅花拍拍灯:
    不错,支持哈..

    zzy90 :
    测试回复

    创享·生活(这个是带链接)

    ============我是可爱的分割线===========
    我也在怀疑是那个编码的问题,但是我已经将主题所有的文件都改成了UTF无BOM(除了functions.php)。这个主题是外国的主题,然后由某一个国人修改的,不知道是什么问题

  11. 我还是有点糊涂 我没有用过这个插件

    可以直接安装后开启就能实现功能吗?

    我现在就是用的自带回复系统和嵌套 想给留言者有邮件提示回复的功能

  12. 拿走了,谢谢啦……

  13. 已经在用了。不错,这是先用第一个的时候,打勾的那个框错位了,和字分了两行,能帮忙解决一下吗??

  14. Clyee ~

    上到一半就下课了

  15. 米修 ~

    添加后不成功,不知道什么原因,主题是xu.hel的istudio

  16. JCBoy ~

    我怎么不行呢?直接添加代码~~导致WP崩溃 出现乱码~~

留下評論

:?: :razz: :sad: :!: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: :smile: :evil:
貼圖 表情 ( ps. 若要貼代碼, 請將 "<" 改成 "&lt;" 即可, 此方法在所有 WP 網站均適用. )

Trackbacks/Pingbacks

  1. 回复的通知你有没有 --- 2010年 09月 03日
  2. 免插件实现评论回复邮件通知 | 竹下无为梦 --- 2010年 08月 23日
  3. 新主题Z-Turn上线测试 _ BoKeam's Blog --- 2010年 08月 18日
  4. 免插件实现WordPress评论回复邮件通知 | 壹品集 --- 2010年 08月 16日
  5. Wordpress之强化后台评论回复页面 |阿邙'S Blog --- 2010年 08月 02日
  6. Ajax Comments 评论回复邮件通知(代码版) - FORECE's 博客日志 --- 2010年 07月 29日
  7. 今晚的心情较好写篇文章,哈哈 | 漂博~livecm心情小解 --- 2010年 07月 23日
  8. 狼图腾 » iNove修改小记 --- 2010年 07月 22日
  9. 成功把Ajax提交评论效果添加到Zbench | 邪罗刹的菠萝阁 --- 2010年 07月 21日
  10. WordPress对回复评论的邮件通知 | 米修网 --- 2010年 07月 15日
  11. Rain|折腾之干掉插件 | 静夜燃香 --- 2010年 07月 14日
  12. WordPress 评论回复邮件通知的实现 « Yet another wordpress blog --- 2010年 07月 09日
  13. WordPress 评论回复邮件通知的实现 --- 2010年 07月 09日
  14. WordPress 评论回复邮件通知的实现 | 芒果 --- 2010年 07月 03日
  15. Dream Ahead-渔人-oBlue.Net » worpress装修日记一:免插件增加评论回复邮件通知 --- 2010年 06月 27日
  16. 新主题Green living上线测试. | 阿邙’S Blog --- 2010年 06月 15日
  17. WordPress 主題製作學習筆記:幫你的主題加上「自帶嵌套評論+Ajax comments」功能 --- 2010年 06月 07日
  18. 聆听大海 » WordPress中不用插件实现评论回复后邮件通知 --- 2010年 06月 05日
  19. 韓德爾の原木 --- 2010年 05月 07日
  20. 评论回复后[邮件通知] – I'm Daley --- 2010年 05月 06日
  21. Comment Mail Notify 改进的评论回复邮件通知 « 可可唯一 i'm keqin --- 2010年 04月 20日
  22. 终于解决了Wordpress在Godaddy主机评论回复邮件通知功能 « 阿邙’S Blog --- 2010年 04月 19日
  23. Ink and wash(水墨古韵)V1.2.0 For Wordpress « 虎頭魚 I'm Arne chen --- 2010年 04月 06日
  24. Ink and wash 更新至1.2.0 « 虎頭魚 I'm Arne chen --- 2010年 04月 06日
  25. WordPress知识资源整理(一) ­ 早过忘川 --- 2010年 04月 04日
  26. wordpress评论回复邮件通知-代码版 – 三头一匹 --- 2010年 03月 15日
  27. 博客小结 « 一起疯! | 17mad's Blog --- 2010年 02月 15日

這篇文章上的評論 RSS feed TrackBack URL