WordPress不用插件实现评论回复邮件通知

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

当你回复了用户的评论,或用户之间的相互回复,通过邮件通知该用户,可以促进网站回流,增加用户黏度。

虽然网络上已经有了许多评论回复邮件通知代码,但感觉大多数都太过复杂了,毕竟我们只想要简单的一个通知功能,所以我自己写了一个。

function bzg_comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
if($parent_id) {
$blogname = get_bloginfo( 'name' );
$adminemail = get_bloginfo('admin_email');
$parent_email = get_comment($parent_id)->comment_author_email;
$parent_name = get_comment($parent_id)->comment_author;
$mail_title = $parent_name . ' 您在' . $blogname . '上的评论有了新回复';
$headers[] = 'From: ' . $blogname . ' < ' . $adminemail . '>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$comment_link = get_comment_link($comment_id, array('type' => 'comment'));
$mail_txt = '';
$mail_txt .= '<p>' . $parent_name . ' 您好!</p>';
$mail_txt .= '<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的评论有了新的回复,您的评论内容为:</p>';
$mail_txt .= '<p>' . get_comment($parent_id)->comment_content . '</p>';
$mail_txt .= '<p>回复内容为:</p>';
$mail_txt .= '<p>' . get_comment($comment_id)->comment_content . '</p>';
$mail_txt .= '<p>您可以点击下面的链接查看详细内容:<br />';
$mail_txt .= '<a href="' . $comment_link . '">' . $comment_link . '</a></p>';
wp_mail( $parent_name . ' <' . $parent_email . '>', $mail_title, $mail_txt, $headers );
}
}
add_action('comment_post', 'bzg_comment_mail_notify');

将以上代码粘贴至主题文件functions.php中即可。