WordPress文章作者回复评论自动发送邮件[升级版]-用户可自定义是否接收邮件提醒

此方式,需要对数据库表添加字段,不了解数据库的不建议使用此方式
数据无价,修改之前,请备份数据库

首先第一步,表wp_comments添加字段comment_mail_notify,字段类型为TINYINT,默认值为0,也可使用下面SQL语句创建

ALTER TABLE wp_comments ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0

第二步,找到当前主题的functions.php文件,添加以下代码

/* 自动加勾选栏 */
add_action('comment_form', 'add_checkbox');
function add_checkbox() {
  echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="true" style="margin-left:20px;" /><label for="comment_mail_notify">作者回复时邮件通知我</label>';
}
/* 评论通知邮件推送 */
add_action('comment_post', 'comment_mail_notify');
function comment_mail_notify($comment_id) {
  $admin_notify = '0'; // 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 : '';
  $to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
  $spam_confirmed = $comment->comment_approved;
  $author_email=get_the_author_meta('user_email', get_post($comment->comment_post_ID)->post_author);
  global $wpdb;
  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';

  if (($parent_id != '') && ($spam_confirmed != 'spam') && ($spam_confirmed != 'trash') && ($to != $admin_email) && ($comment_author_email == $author_email) && $notify == '1') {
    /* 上面的判断式,决定发出邮件的必要条件:
    ($parent_id != '') && ($spam_confirmed != 'spam'): 回复的, 而且不是 spam 才可发, 必需!!
    ($spam_confirmed != 'trash') 非回收站的
    ($to != $admin_email) : 不发给 admin.
    ($comment_author_email == $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="'.get_permalink($comment->comment_post_ID).'" title="'.get_the_title($comment->comment_post_ID).'">查看</a> 回复的完整內容,欢迎您再次访问 <a href="https://www.jssolo.com" title="技术SOLO">技术SOLO</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 );
  }
}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 共1条

请登录后发表评论