WordPress免插件评论邮件通知代码

WordPress8876阅读模式

WordPress评论回复邮件通知,最大好处就是可以增加访问者对博客的粘性。

一、有勾选栏, 由访客决定是否要回应邮件通知

  1. /* comment_mail_notify v1.0 by willin kan. (有勾選欄, 由訪客決定) */
  2. function comment_mail_notify($comment_id) {
  3.   $admin_notify = '1'; // admin 要不要收回覆通知 ( '1'=要 ; '0'=不要 )
  4.   $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
  5.   $comment = get_comment($comment_id);
  6.   $comment_author_email = trim($comment->comment_author_email);
  7.   $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  8.   global $wpdb;
  9.   if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
  10.     $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
  11.   if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
  12.     $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
  13.   $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
  14.   $spam_confirmed = $comment->comment_approved;
  15.   if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
  16.     $wp_email = 'no-reply@' . preg_replace('#^www\.#', ''strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
  17.     $to = trim(get_comment($parent_id)->comment_author_email);
  18.     $subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
  19.     $message = '
  20.     <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; border-radius:5px;">
  21.       <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  22.       <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
  23.        . trim(get_comment($parent_id)->comment_content) . '</p>
  24.       <p>' . trim($comment->comment_author) . ' 給您的回應:<br />'
  25.        . trim($comment->comment_content) . '<br /></p>
  26.       <p>您可以點擊 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</a></p>
  27.       <p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
  28.       <p>(此郵件由系統自動發出, 請勿回覆.)</p>
  29.     </div>';
  30.     $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
  31.     $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  32.     wp_mail( $to$subject$message$headers );
  33.     //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  34.   }
  35. }
  36. add_action('comment_post', 'comment_mail_notify');
  37. /* 自動加勾選欄 */
  38. function add_checkbox() {
  39.   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>';
  40. }
  41. add_action('comment_form', 'add_checkbox');
  42. // -- END ----------------------------------------

二、无勾选栏, 由管理者决定在什麽条件下发邮件

  1. /* comment_mail_notify v1.0 by willin kan. (無勾選欄) */
  2. function comment_mail_notify($comment_id) {
  3.   $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改為你指定的 e-mail.
  4.   $comment = get_comment($comment_id);
  5.   $comment_author_email = trim($comment->comment_author_email);
  6.   $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  7.   $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
  8.   $spam_confirmed = $comment->comment_approved;
  9.   if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email) && ($comment_author_email == $admin_email)) {
  10.     /* 上面的判斷式,決定發出郵件的必要條件:
  11.     ($parent_id != '') && ($spam_confirmed != 'spam'): 回覆的, 而且不是 spam 才可發, 必需!!
  12.     ($to != $admin_email) : 不發給 admin.
  13.     ($comment_author_email == $admin_email) : 只有 admin 的回覆才可發.
  14.     可視個人需求修改以上條件.
  15.     */
  16.     $wp_email = 'no-reply@' . preg_replace('#^www\.#', ''strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
  17.     $subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
  18.     $message = '
  19.     <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; border-radius:5px;">
  20.       <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  21.       <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
  22.        . trim(get_comment($parent_id)->comment_content) . '</p>
  23.       <p>' . trim($comment->comment_author) . ' 給您的回應:<br />'
  24.        . trim($comment->comment_content) . '<br /></p>
  25.       <p>您可以點擊 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</a></p>
  26.       <p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
  27.       <p>(此郵件由系統自動發出, 請勿回覆.)</p>
  28.     </div>';
  29.     $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
  30.     $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  31.     wp_mail( $to$subject$message$headers );
  32.     //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  33.   }
  34. }
  35. add_action('comment_post', 'comment_mail_notify');
  36. // -- END ----------------------------------------

三、所有回覆都发邮件

  1. /* comment_mail_notify v1.0 by willin kan. (所有回覆都發郵件) */
  2. function comment_mail_notify($comment_id) {
  3.   $comment = get_comment($comment_id);
  4.   $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  5.   $spam_confirmed = $comment->comment_approved;
  6.   if (($parent_id != '') && ($spam_confirmed != 'spam')) {
  7.     $wp_email = 'no-reply@' . preg_replace('#^www\.#', ''strtolower($_SERVER['SERVER_NAME'])); //e-mail 發出點, no-reply 可改為可用的 e-mail.
  8.     $to = trim(get_comment($parent_id)->comment_author_email);
  9.     $subject = '您在 [' . get_option("blogname") . '] 的留言有了回應';
  10.     $message = '
  11.     <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; border-radius:5px;">
  12.       <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
  13.       <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />'
  14.        . trim(get_comment($parent_id)->comment_content) . '</p>
  15.       <p>' . trim($comment->comment_author) . ' 給您的回應:<br />'
  16.        . trim($comment->comment_content) . '<br /></p>
  17.       <p>您可以點擊 <a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</a></p>
  18.       <p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
  19.       <p>(此郵件由系統自動發出, 請勿回覆.)</p>
  20.     </div>';
  21.     $from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
  22.     $headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
  23.     wp_mail( $to$subject$message$headers );
  24.     //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  25.   }
  26. }
  27. add_action('comment_post', 'comment_mail_notify');
  28. // -- END ----------------------------------------

使用方法:添加到在主题的function.php中

这个代码在评论分页的 get_comment_link() 有个 bug。

对使用 comments 和 trackbacks/pingbacks 分离的情况才会出现,没分离的没有影响.
当直接调用 get_comment_link() 因为没经过 wp_list_comments(‘type=comment’) 函数,它会以所有的评论作为分页对象. 所以 trackbacks/pingbacks 数量多的时候会让 cpage 多算了, 本来是 cpage=7 会成了 cpage=8, 所以点击邮件里的“查看回应完整内容会找不到正确评论页面”.

— 修正方式 —-

将以上的:
get_comment_link($parent_id)

改为:
get_comment_link($parent_id, array('type' => 'comment'))
加入的参数是让它选取 comment 数量计算就好.
评论样式有使用 comments 和 trackbacks/pingbacks 分离的童鞋,请进行修改,没用到的就不用改了。

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
评论  8  访客  7  作者  1
    • MTnetwork
      MTnetwork 2

      鸟哥 按照上面的方法把代码function.php中去 整个后台全部乱码, 最新4.0版
      请问要实现回复留言 邮件通知 评论者怎么实现啊 ,就像您站这样

      • MTnetwork
        MTnetwork 2

        鸟哥 按照上面的方法把代码function.php中去 整个后台全部乱码, 最新4.0版
        请问要实现回复留言 邮件通知 评论者怎么实现啊 ,就像您站这样

        • 小岩网络
          小岩网络 1

          鸟哥,“无法发送电子邮件,您的主机禁用了 mail() 函数”和“抱歉,key似乎无效”,这两个问题都解决了。。但是无论在函数文件添加代码,还是使用插件,都不能解决评论回复邮件通知。。。求帮助啊!

          • 小岩网络
            小岩网络 1

            折腾好了!解决了1

            • 米粒博客
              米粒博客 1

              挺好的!

              • 黎叔
                黎叔 0

                :cry: :cry: :cry: 鸟哥,,这个在BEGIN中好像还是发不了,PHP也开了FS函数,inc/notify.php里面的no-reply@也改成了可用邮箱,然而根本就发不出去

              匿名

              发表评论

              匿名网友
              :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

              拖动滑块以完成验证