WordPress 博客非插件实现评论回复邮件通知功能

yibin 2016-06-01 WordPress 551

使用WordPress 博客要想在第一时间知道有人在自己博客留言,然后在第一时间回复留言再第一时间通知邮件者,这就需要WordPress博客拥有一个通知的功能,现在有的就是邮件通知功能。实现邮件通知最简单的方法就是直接使用插件,但是更多人喜欢不用插件,博客吧介绍下不用插件实现评论回复邮件通知的效果

评论邮件通知的方法:

  • 登陆博客后台,点击“外观”选项卡下的“编辑”选项进入主题编辑界面
  • 选项functions.php文件,实现所有回复都发送邮件通知,就在<?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
    28
    
    /* 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>您可以点击 <a href="'
    . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复完整內容</a></p>
    <p>欢迎再度光临 <a href="'
    . get_option('home') . '">' . 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 ); //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing } } add_action('comment_post', 'comment_mail_notify'); // -- END ----------------------------------------
  • 如果想让访客自己选择是否邮件通知,那么就在functions.php文件中的<?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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    
    /* 开始*/
    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>您可以點擊 <a href="'
    . htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</a></p>
    <p>歡迎再度光臨 <a href="'
    . get_option('home') . '">' . 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 ); //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;" /><label for="comment_mail_notify">有人回覆時郵件通知我</label>'; } add_action('comment_form', 'add_checkbox');
  • 如果想让博客管理员决定什么情况下发邮件,就在functions.php文件中的<?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
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    /* 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>您可以點擊 <a href="'
    . htmlspecialchars(get_comment_link($parent_id)) . '">查看回應完整內容</a></p>
    <p>歡迎再度光臨 <a href="'
    . get_option('home') . '">' . 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 ); //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing } } add_action('comment_post', 'comment_mail_notify'); // -- END ----------------------------------------

原作者:http://kan.willin.org/

提醒:其实WordPress博客本身具有邮件通知评论的功能,在设置选项卡下的讨论选项里设置,但可能是WordPress目前的一个bug,无法实现邮件通知,上好像有解决的方法,慢慢博客吧将会寻找解决之道。

扫码添加微信

13013082126 扫描微信 建站咨询 优化咨询