欢迎光临
我们一直在努力

WordPress添加评论回复邮件提醒通知功能

建站超值云服务器,限时71元/月

本文目录
[隐藏]

  • 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@'%20. preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); //e-mail 发出点, no-reply 可改为可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 ['%20. 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>'%20. trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
      <p>您曾在《'%20. get_the_title($comment->comment_post_ID) . '》的留言:<br />'
       . trim(get_comment($parent_id)->comment_content) . '</p>
      <p>'%20. trim($comment->comment_author) . ' 给您的回复:<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以点击 查看回复完整內容</p>
      <p>欢迎再度光临 '%20. 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 != ''%20&& $spam_confirmed != 'spam'%20&& $notify == '1') {
    $wp_email = 'no-reply@'%20. preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 ['%20. 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>'%20. trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
      <p>您曾在《'%20. get_the_title($comment->comment_post_ID) . '》的留言:<br />'
       . trim(get_comment($parent_id)->comment_content) . '</p>
      <p>'%20. trim($comment->comment_author) . ' 给您的回复:<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以点击查看回复的完整內容</p>
      <p>还要再度光临 '%20. 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@'%20. preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
    $subject = '您在 ['%20. 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>'%20. trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
      <p>您曾在《'%20. get_the_title($comment->comment_post_ID) . '》的留言:<br />'
       . trim(get_comment($parent_id)->comment_content) . '</p>
      <p>'%20. trim($comment->comment_author) . ' 给您的回复:<br />'
       . trim($comment->comment_content) . '<br /></p>
      <p>您可以点击 查看回复的完整內容</p>
      <p>还要再度光临 '%20. 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'%20&& $to != $admin_email && $to != $author_email) {
         $wp_email = 'no-reply@'%20. preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
         $subject = '您在 ['%20. 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>'%20. trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
             <p>您曾在《'%20. get_the_title($comment->comment_post_ID) . '》的留言:<br />'
             . trim(get_comment($parent_id)->comment_content) . '</p>
             <p>'%20. trim($comment->comment_author) . ' 给你的回复:<br />'
             . trim($comment->comment_content) . '<br /></p>
             <p>您可以点击 <a href="'%20. htmlspecialchars(get_comment_link($parent_id,array("type" => "all"))) . '">查看回复的完整內容</a></p>
             <p>欢迎再度光临 <a href="'%20. get_option('home') . '">'%20. 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=”‘%20.%20htmlspecialchars(get_comment_link($parent_id,array(“type” => “all”))) . ‘”>查看回复的完整內容</a></p> <p>欢迎再度光临 <a href=”‘%20.%20get_option(‘home’)%20.%20′”>’ . 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 ); } }

备注说明

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » WordPress添加评论回复邮件提醒通知功能
分享到: 更多 (0)