从Typecho转到了WordPress,但是感觉评论回复通知不好看。于是就弄了一个Wordpress评论回复通知美化模板。

1、首先关闭自带的回复样式,要不然会有两人。在“设置” –> “讨论”中把下图标红部分的钩去掉。

WordPress邮件通知系统美化版,包含功能如下

  • 1、评论审核通知管理员
  • 2、有新留言时通知管理员
  • 3、评论回复通知用户

使用方法

把下面代码加到当前主题的functions.php文件里面。

//WordPress 评论回复邮件通知代码
function comment_mail_notify($comment_id) {
	$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 = '[email protected]' . 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: #f59200;font-weight: bold;">&gt; </span>您在 <a style="text-decoration:none; color:#f59200;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:#f59200" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回复內容</a>,也欢迎再次光临 <a style="text-decoration:none; color:#f59200"
			href="' . home_url() . '">' . $blogname . '</a>。祝您生活愉快!</p>
			<p style="padding-bottom: 15px;">(此邮件由系统自动发出,请勿直接回复!)</p></div></div></td></tr></tbody></table></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: #f59200;font-weight: bold;">&gt; </span><a style="text-decoration:none;color: #f59200;" 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:#f59200" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回复內容</a>,也欢迎再次光临 <a style="text-decoration:none; color:#f59200" 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: #f59200;font-weight: bold;">&gt; 「 </span><a style="text-decoration:none;color: #f59200;" 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: #f59200;" 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: #12ADDB;" href="'. admin_url( "comment.php?action=approve&c={$comment_id}#wpbody-content" ) . '">[批准评论]</a> | <a style="text-decoration:none;color: #12ADDB;" href="'. admin_url( "comment.php?action=trash&c={$comment_id}#wpbody-content" ) . '">[移至回收站]</a>。您还可以:<a style="text-decoration:none; color:#12ADDB" href="' . admin_url( "comment.php?action=delete&c={$comment_id}#wpbody-content" ) . '">永久删除评论</a> | <a style="text-decoration:none;color: #12ADDB;" href="'. admin_url( "comment.php?action=spam&c={$comment_id}#wpbody-content" ) . '">标记为垃圾评论</a>
			<p>当前有 ' . $comments_waiting . ' 条评论等待审核。请移步<a style="text-decoration:none;color: #f59200;" href="' . admin_url('edit-comments.php?comment_status=moderated#wpbody-content') . '">审核页面</a>来查看。</p>也欢迎再次光临 <a style="text-decoration:none; color:#f59200" 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');

演示图