WordPress 评论回复邮件通知

wordpress评论回复邮件通知代码,仅作记录。添加到functions.php里面即可。

/*
 * comment notify
 * ====================================================
 */
 add_action('comment_post','comment_mail_notify');
 function comment_mail_notify($comment_id) {
 $admin_notify = '1';
 $admin_email = get_bloginfo ('admin_email');
 $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']));
 $to = trim(get_comment($parent_id)->comment_author_email);
 $subject = 'Hi,您在 [' . get_option("blogname") . '] 的留言有人回复啦!';
 $message = '
 <div style="color:#333;font:100 14px/24px microsoft yahei;">
 <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
 <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br /> &nbsp;&nbsp;&nbsp;&nbsp; '
 . trim(get_comment($parent_id)->comment_content) . '</p>
 <p>' . trim($comment->comment_author) . ' 给您的回应:<br /> &nbsp;&nbsp;&nbsp;&nbsp; '
 . 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 style="color:#999">(此邮件由系统自动发出,请勿回复.)</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 );
 }
 }
 /*
 * comment mail notify checked
 * ====================================================
 */
 add_action('comment_form','hui_add_checkbox');
 function hui_add_checkbox() {
 echo '<label for="comment_mail_notify" class="checkbox inline hide" style="padding-top:0"><input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked"/>'.__('有人回复时邮件通知我', 'haoui').'</label>';
 }

发表评论

电子邮件地址不会被公开。 必填项已用*标注