WordPress评论回复邮件提醒

WordPress21K阅读模式

根据自己的需要选择,将下面的代码添加到当前主题的functions.php文件中即可:

方法一:所有回复都发送邮件通知

默认所有填写了邮箱的评论都将发邮件提醒评论人,没有任何勾选设置。

  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;">
  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>您可以点击 查看回复完整內容</p>
  18.       <p>欢迎再度光临 ' . get_option('blogname') . '</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.   }
  25. }
  26. add_action('comment_post', 'comment_mail_notify');

方法二:让访客自己选择是否邮件通知

在评论框下方显示一个勾选框,让评论人自己决定是否接收邮件通知。

  1. function comment_mail_notify($comment_id) {
  2.   $admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
  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.   global $wpdb;
  8.   if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
  9.     $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
  10.   if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
  11.     $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
  12.   $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
  13.   $spam_confirmed = $comment->comment_approved;
  14.   if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
  15.     $wp_email = 'no-reply@' . preg_replace('#^www.#', ''strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
  16.     $to = trim(get_comment($parent_id)->comment_author_email);
  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;">
  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>您可以点击查看回复的完整內容</p>
  26.       <p>还要再度光临 ' . get_option('blogname') . '</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.   }
  33. }
  34. add_action('comment_post', 'comment_mail_notify');
  35. /* 自动加勾选栏 */
  36. function add_checkbox() {
  37.   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>';
  38. }
  39. add_action('comment_form', 'add_checkbox');

方法三:让博客管理员决定什么情况下发邮件

调整下面的代码,选择发送回复邮件

  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;">
  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>您可以点击 查看回复的完整內容</p>
  26.       <p>还要再度光临 ' . get_option('blogname') . '</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.   }
  33. }
  34. add_action('comment_post', 'comment_mail_notify');

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

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  2  访客  2
    • sdaf
      sdaf 0

      sdfg为什么我的就回复没法有邮箱提示

      • Oeasy
        Oeasy 3

        添加代码后网站打不开了

      匿名

      发表评论

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

      拖动滑块以完成验证