1557571545 Comment Plugin Min

给WordPress添加评论邮件回复通知的两种方法

WordPress评论邮件通知奶爸觉得是一个非常有用的功能,能够增加你和网友的互动。

试想一下,如果你在本文问我:“我跟着你的教程操作了,怎么评论还是没有邮件通知呢?”,你是愿意每天都来刷新几遍文章看我回复你没有,还是愿意当我评论你过后发送邮件告诉你,嗨,你的评论有人回复了哦。

代码实现方法

有两种方法可以实现WordPress博客评论后给评论者发送邮件的功能,一种是通过增加代码来实现,另外一种就是通过插件来实现。

如果使用代码功能,基本上都是用的下面这个代码,或者稍微修改过的版本

/* comment_mail_notify v1.0 by willin kan. (所有回复都发邮件) */

登陆博客后台,点击“外观”选项卡下的“编辑”选项进入主题编辑界面,在functions.php文件中的之间添加以下函数即可:

1、所有人都发邮件

/* 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=" 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 );
    //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  }
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------

2、让访客自己选择是否邮件通知
在functions.php文件中的之间添加以下函数,该函数将会在评论框底部生成要不要收回复通知的选项

/* 开始*/
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=" 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 );
    //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;" /><span for="comment_mail_notify">有人回复时邮件通知我</span>';
}
add_action('comment_form', 'add_checkbox');

3、让博客管理员决定什么情况下发邮件

在functions.php文件中的之间添加以下函数:

/* 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=" 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 );
    //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  }
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------

对代码感兴趣的可以点击这里去申杰博客看看,写得还算详细。

不过这个代码在部分主题里面不会起作用,所以奶爸推荐使用Wenprise Better Emails这款WordPress评论邮件通知插件来解决。

插件版方法(推荐)

WordPress评论邮件通知

Wenprise Better Emails可以美化 WordPress 评论审核通知邮件,评论通知文章作者的邮件,增加回复评论时,发送邮件通知给评论作者的功能。

奶爸推荐这款Wenprise Better Emails插件的原因有以下几点:

  1. 能实现评论时邮件回复通知,安装即用,适合新手;
  2. 国人制作的插件,符合使用习惯,界面也漂亮;
  3. 带有取消订阅功能,能减少邮件被判定为垃圾邮件的几率。

插件安装地址:https://wordpress.org/plugins/wenprise-better-emails/

代码和插件都不能用?

使用评论邮件通知有一个前提条件,就是你的服务器可以发送邮件。

而实际上大多数主机都不支持直接发送邮件的,所以我们需要借助第三方插件来实现主机发送邮件,也就是使用SMTP的方式来发送邮件,你可以参考下面的文章。

我使用过的WordPress邮件插件推荐

另外,Comment Approved这款插件奶爸也用过,是评论审核后给对方发邮件告诉他评论审核通过了。

 

5/5 - (2 votes)
滚动至顶部