A-A+

wordpress评论邮件通知

2015年01月04日 编程技术 暂无评论

如果有访客在你的站点进行了文章的评论,这个时候如果我们进行了回复,怎样让访客知道自己的留言有人进行了回复呢?这就需要 wordpress 的邮件回复通知功能了,在当我们回复了用户之后,就进行一篇邮件的回复,这对于回访来说能起到特别好的效果,实现的代码如下:

//评论邮件通知
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)) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發出點, no-reply 可改為可用的 e-mail.
$subject = '您在 [' . get_option("blogname") . '] 的评论有新的回复';
$message = ' //04IE.COM
<div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px; border-radius:5px;">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在 [' . get_option("blogname") . '] 的文章 《' . get_the_title($comment->comment_post_ID) . '》 上发表评论:<br />'
. nl2br(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回复如下:<br />'
. nl2br($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>(此郵件由系統自動發出, 請勿回覆.)</p>
</div>';
$message = convert_smilies($message);
$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');

将本代码放入到主题 functions.php 的下面即可。

标签:

给我留言