×
您的位置: 首页 > 编程笔记

WordPress评论回复邮件通知对方功能

WordPress Word 时间:2014-11-28  查看:558   收藏

原本小z博客使用了多说评论,回复对方后会收到多说的提示,不过近期Gravatar头像被墙,多说服务器也太不稳定,部分头像回源而不能缓存,导致博客加载异常缓慢,还没有解决的请看这里

因此最近又换回了wordpress自带评论,回归简单化,速度感觉提升了不少,不过为了利于用户体验,当别人在我们博客留言我们回复对方的时候希望对方能够收到一条通知,那么就可以使用评论回复邮件通知的功能。

网上提供了很多种代码的方法,我也试过好几种,有的代码已经失效不再起作用,而有的代码则不太完整,最后由热心网友无主题提供了一段不错的功能代码,只需要将代码添加到主题目录的functions.php中即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 评论回应邮件通知
function comment_mail_notify($comment_id) {
  $admin_email = get_bloginfo ("admin_email"); 
  $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)) {
	$wp_email = "no-reply@" . preg_replace("#^www.#", "", strtolower($_SERVER["SERVER_NAME"]));
	$subject = "您在 [" . get_option("blogname") . "] 的留言有了新回复";
	$message = "
		

>您在 <a style="text-decoration:none;color: #12ADDB;" href="" <span style=" color:="" 339933="" span="" get_option="" home="" target="_blank">" . get_option("blogname") . " 博客上的留言有回复啦!

亲爱的 " . trim(get_comment($parent_id)->comment_author) . ", 您好!您曾在文章《" . get_the_title($comment->comment_post_ID) . "》上发表评论:

". trim(get_comment($parent_id)->comment_content) . "

". trim($comment->comment_author) ."给您的回复如下:

" . trim($comment->comment_content) ."

您可以点击<a href="" " . get_option("blogname") . "

请注意:此邮件由 <a href="" <span style=" color:="" 339933="" span="" get_option="" style="color: #009900;" home="">" . get_option("blogname") . " 自动发送,请勿直接回复。 如果此邮件不是您请求的,请忽略并删除!

"; $from = "From: "" . get_option("blogname") . "" <$wp_email>"; $headers = "$from Content-Type: text/html; charset=" . get_option("blog_charset") . " "; wp_mail( $to, $subject, $message, $headers ); } } add_action("comment_post", "comment_mail_notify"); // -- END ----------------------------------------

显示的效果请看下面的截图,如果你的主机禁用了mail()函数,建议使用WP SMTP插件或使用SMTP代码来解决。

huifu

邮件回复通知

 

0% (0)
0% (0)