WordPress非插件实现评论回复邮件提醒通知

要想在第一时间知道有人在本身博客留言,而后在第一时间回复留言再第一时间通知邮件者,这就须要WordPress博客拥有邮件通知功能。
先看效果:

评论邮件通知的方法:
1.全部回复都发送邮件通知
登录博客后台,点击“外观”选项卡下的“编辑”选项进入主题编辑界面,在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= " 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文件中的之间添加如下函数,该函数将会在评论框底部生成要不要收回复通知的选项html

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= " 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' );

上面最后一句web

1
<span for = "comment_mail_notify" >有人回复时邮件通知我</span>';

网上原来的代码是app

1
<label for = "comment_mail_notify" >有人回复时邮件通知我</label>';

只是由于label是块元素,会自动换行,效果就是文字在复选框的下面,因此我为了让文字显示在复选框的右边,就使用了内联元素span,这样就不会自动换行了。
3.让博客管理员决定什么状况下发邮件
在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
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= " 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 ----------------------------------------

根据你的须要随意选择一种,我使用的是第二种。post