经过将近两年的使用整体感觉还算不错,但是很多功能我都用不到,我想用的功能却没有,例如
解决方法
子比主题自带的评论审核模板样式还算可以,为了保持一致性就决定两者结合,把原来邮件模板的语序重新调整并使用上边可用的函数对评论者姓名、评论内容、评论时间、回复等信息进行调用最终得到了如下代码:
//访客评论邮件回复
add_action('comment_post', 'comment_mail_notify', 99);
add_action('comment_unapproved_to_approved', 'comment_mail_notify', 99);
function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);
if ($comment->comment_approved != '1') return;//如果评论未通过审核,return
$admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
$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)) {
/* 上面的if判断是决定是否发出邮件的关键:
($parent_id != '') && ($spam_confirmed != 'spam'): 评论是被回复的, 而且不是 spam 才可以发(必需!!)
($to != $admin_email) : 不给 admin发邮件(可选).
($comment_author_email == $admin_email) : 只有 admin 的回复才发邮件(可选).
可根据个人需要修改上面的条件.
*/
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));//发件人 e-mail 地址,no-reply 可改为可用的 e-mail
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = '您在 [' . get_option("blogname") . '] 的留言有了新的回复';//邮件主题
$message = '您好!' . trim(get_comment($parent_id)->comment_author) . ',';//邮件内容
$message .= '您于'. trim(get_comment($parent_id)->comment_date) . ' ';
$message .= '在《' . get_the_title($comment->comment_post_ID) . '》中发表评论:' . '</br>';
$message .= '<div style="padding: 10px 15px; border-radius: 8px; background: rgba(141, 141, 141, 0.05); line-height: 1.7;">' . nl2br(get_comment($parent_id)->comment_content) . '</div>';
$message .= '</br>';
$message .= '' . trim($comment->comment_author) . '';
$message .= ' 于'. trim($comment->comment_date) . ' 给您的回复如下:</br>';
$message .= '<div style="padding: 10px 15px; border-radius: 8px; background: rgba(141, 141, 141, 0.05); line-height: 1.7;">' . nl2br($comment->comment_content) . '</div>';
$message .= '</br>';
$message .= '您可以点击下方按钮查看回复内容</br>';
$message .= '<a target="_blank" style="margin-top: 20px;padding:5px 20px" class="but jb-blue" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复</a>' . "</br>";
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
//wp_mail发送邮件
wp_mail( $to, $subject, $message, $headers );
}
}
将此代码放在主题根目录的functions.php即可,为了不产生多余的邮件主题设置里边的发送邮件被我全部关闭了,经过测试目前这种方案能够覆盖所有评论场景,很完美,最终效果图如下。
修改后效果
最后的2个小功能下一篇文章再讲吧!!!
感谢您的来访,获取更多精彩文章请收藏本站。
© 版权声明
THE END
暂无评论内容