共计 6604 个字符,预计需要花费 17 分钟才能阅读完成。
美化模版包含功能如下
-
评论审核通知管理员
-
有新留言时通知管理员
-
评论回复通知用户
-
用户可勾选是否接收邮件通知– 有bug,目前在调试
优化后的示例
新评论通知站长:
待审核评论:
评论回复通知评论者:
实现方法
添加以下代码至主题的function中
//WordPress 评论回复邮件通知代码
function comment_mail_notify($comment_id) {
$admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
$admin_email = get_bloginfo ('admin_email');
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
global $wpdb;
$comments_waiting = $wpdb->get_var("SELECT count(comment_ID) FROM $wpdb->comments WHERE comment_approved = '0'");
if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email)) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . $blogname . ']' . ' 中的留言有了新回复!';
$message = '
<div style="background-color:white;border-left: 2px solid #555555;box-shadow:0 1px 3px #AAAAAA;line-height:180%;padding:0 15px 12px;width:500px;margin:50px auto;color:#555555;font-family:"Source Sans Pro","Hiragino Sans GB","Microsoft Yahei",SimSun,Helvetica,Arial,Sans-serif,monospace;font-size:14px;">
<h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:13px 0 10px 8px;">
<span style="color: #409eff;font-weight: bold;">> </span>
您在 <a style="text-decoration:none; color:#409eff;font-weight:600;" href="' . home_url() . '">' . $blogname . '</a> 的留言有回复啦!
</h2>
<div style="font-size: 14px; color: #777; padding: 0 10px; margin-top: 18px;">
<p><b>' . trim(get_comment($parent_id)->comment_author) . '</b> 同学,您曾在文章<b>《' . get_the_title($comment->comment_post_ID) . '》</b>上发表评论:</p>
<p style="background: #F5F5F5; padding: 10px 15px; margin: 18px 0;">' . nl2br(strip_tags(get_comment($parent_id)->comment_content)) . '</p>
<p>' . '<b>' . trim($comment->comment_author) . '</b>'. ' 给您的回复如下:</p>
<p style="background: #F5F5F5; padding: 10px 15px; margin: 18px 0;">' . nl2br(strip_tags($comment->comment_content)) . '</p>
<p>您可以点击 <a style="text-decoration:none; color:#409eff" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回复內容</a>,也欢迎再次光临 <a style="text-decoration:none; color:#409eff"
href="' . home_url() . '">' . $blogname . '</a>。祝您生活愉快!</p>
<p style="padding-bottom: 15px;">(此邮件由系统自动发出,请勿直接回复!)</p>
</div>
</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 );
}
//文章有新评论时通知管理员
if ($parent_id == '' && (trim($comment->comment_author_email) != trim($admin_email)) && ($spam_confirmed != 'spam') && ($comment->comment_approved != 0)){
$wp_email = '';
$subject = '在「' . $blogname .'」的文章《'. get_the_title($comment->comment_post_ID) .'》一文有新的评论';
$message = '
<div style="background-color:white;border-left: 2px solid #555555;box-shadow:0 1px 3px #AAAAAA;line-height:180%;padding:0 15px 12px;width:500px;margin:50px auto;color:#555555;font-family:"Source Sans Pro","Hiragino Sans GB","Microsoft Yahei",SimSun,Helvetica,Arial,Sans-serif,monospace;font-size:14px;">
<h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:13px 0 10px 8px;">
<span style="color: #409eff;font-weight: bold;">> </span>
<a style="text-decoration:none;color: #409eff;" href="' . home_url() . '">' . $blogname . '</a> 博客有新的评论啦!
</h2>
<div style="padding:0 12px 0 12px;margin-top:18px;">
<p><b>'. $comment->comment_author . '</b> 同学在文章<b>《' . get_the_title($comment->comment_post_ID) . '》</b>上发表评论:</p>
<p style="background-color: #f5f5f5;border: 0px solid #DDD;padding: 10px 15px;margin:18px 0;">' . $comment->comment_content . '</p>
<p>您可以点击 <a style="text-decoration:none; color:#409eff" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回复內容</a>,也欢迎再次光临 <a style="text-decoration:none; color:#409eff" href="' . home_url() . '">' . $blogname . '</a>。祝您生活愉快!</p>
</div>
</div>';
$headers = "Content-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $admin_email, $subject, $message, $headers );
}
//评论需要审核时通知
if ($parent_id == '' && (trim($comment->comment_author_email) != trim($admin_email)) && ($spam_confirmed != 'spam') && ($spam_confirmed != 'trash') && ($comment->comment_approved == 0)){
$wp_email = '';
$subject = '在「' . $blogname .'」的文章《' . get_the_title($comment->comment_post_ID) . '》中有新的评论需要审核';
$message = '
<div style="background-color:white;border-left: 2px solid #555555;box-shadow:0 1px 3px #AAAAAA;line-height:180%;padding:0 15px 12px;width:500px;margin:50px auto;color:#555555;font-family:"Source Sans Pro","Hiragino Sans GB","Microsoft Yahei",SimSun,Helvetica,Arial,Sans-serif,monospace;font-size:14px;">
<h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:13px 0 10px 8px;">
<span style="color: #409eff;font-weight: bold;">> 「 </span>
<a style="text-decoration:none;color: #409eff;" href="' . home_url() . '">' . $blogname . '」</a> 中有一条评论等待您的审核
</h2>
<div style="padding:0 12px 0 12px;margin-top:18px;">
<p><b>'. $comment->comment_author . '</b> 同学在文章<b><a style="text-decoration:none;color: #409eff;" href="' . get_permalink($comment->comment_post_ID) . '">《' . get_the_title($comment->comment_post_ID) . '》</a></b>上发表评论:</p>
<p style="background-color: #f5f5f5;border: 0px solid #DDD;padding: 10px 15px;margin:18px 0;">' . $comment->comment_content . '</p>
<p><a style="text-decoration:none;color: #007017;" href="'. admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) . '">[批准评论]</a> | <a style="text-decoration:none;color: #b32d2e;" href="'. admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) . '">[移至回收站]</a>。您还可以:<a style="text-decoration:none; color:#b32d2e" href="' . admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) . '">永久删除评论</a> | <a style="text-decoration:none;color: #b32d2e;" href="'. admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) . '">标记为垃圾评论</a>
<p>当前有 ' . $comments_waiting . ' 条评论等待审核。请移步<a style="text-decoration:none;color: #409eff;" href="' . admin_url('edit-comments.php?comment_status=moderated#wpbody-content') . '">审核页面</a>来查看。</p>也欢迎再次光临 <a style="text-decoration:none; color:#409eff" href="' . home_url() . '">' . $blogname . '</a>。祝您生活愉快!</p>
</div>
</div>';
$headers = "Content-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $admin_email, $subject, $message, $headers );
}
}
add_action('comment_post', 'comment_mail_notify');
/* 自动加勾选栏 --该功能目前有Bug,还在调试,请勿添加 */
/**
function add_checkbox() {
echo '<span class="mail_notify_box" id="mail_notify_box" title="是否接收评论回复邮件通知"><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></span>';
}
add_action('comment_form', 'add_checkbox');
**/
如果收到重复的邮件(比如同时收到系统原始邮件和新修改的邮件),可以关闭下图的设置
正文完