WordPress留言评论自动邮件通知功能实现方法
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

WordPress默认主题中用户留言评论,如果我们去回复之后,或者我们用户评论的时候也不会自动通知博主的。我们可以添加评论邮件通知,这样可以及时的与用户进行互动。当然,有很多插件可以实现这样的功能,在这篇文章中,蜗牛收集到的办法是不用插件,直接在当前主题Functions.php文件中加入代码就可以实现。
第一、自动默认全部通知
// 评论回应邮件通知
function comment_mail_notify($comment_id) {
$admin_notify = \\’1\\’; // admin 要不要收回复通知 ( \\’1\\’=要 ; \\’0\\’=不要 )
$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 : \\’\\’;
global$wpdb;
if ($wpdb->query(\\”Describe {$wpdb->comments} comment_mail_notify\\”) == \\’\\’)
$wpdb->query(\\”ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;\\”);
if (($comment_author_email != $admin_email && isset($_POST[\\’comment_mail_notify\\’])) || ($comment_author_email == $admin_email && $admin_notify == \\’1\\’))
$wpdb->query(\\”UPDATE {$wpdb->comments} SET comment_mail_notify=\\’1\\’ WHERE comment_ID=\\’$comment_id\\’\\”);
$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : \\’0\\’;
$spam_confirmed = $comment->comment_approved;
if ($parent_id != \\’\\’ && $spam_confirmed != \\’spam\\’ && $notify == \\’1\\’) {
$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 = \\’
<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;\\”>
<p>\\’ . trim(get_comment($parent_id)->comment_author) . \\’, 您好!</p>
<p>您曾在《\\’ . get_the_title($comment->comment_post_ID) . \\’》的留言:<br />\\’
. trim(get_comment($parent_id)->comment_content) . \\\'</p>
<p>\\’ . trim($comment->comment_author) . \\’ 给您的回应:<br />\\’
. trim($comment->comment_content) . \\\'<br /></p>
<p>您可以点击 <a h=\\”\\’ . htmlspecialchars(get_comment_link($parent_id)) . \\’\\”>查看回应完整內容</a></p>
<p>欢迎您再度光临 <a h=\\”\\’ . get_option(\\’home\\’) . \\’\\”>\\’ . get_option(\\’blogname\\’) . \\\'</a></p>
<p>(此邮件由系统自动发出,请勿回复.)</p>
</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 );
//echo \\’mail to \\’, $to, \\\'<br/> \\’ , $subject, $message; // for testing
}
}
add_action(\\’comment_post\\’, \\’comment_mail_notify\\’);
// 自动勾选
function add_checkbox() {
echo \\\'<input type=\\”checkbox\\” name=\\”comment_mail_notify\\” id=\\”comment_mail_notify\\” value=\\”comment_mail_notify\\” checked=\\”checked\\” style=\\”margin-left:0px;\\” /><label for=\\”comment_mail_notify\\”>有人回复时邮件通知我</label>\\’;
}
add_action(\\’comment_form\\’, \\’add_checkbox\\’);
第二、用户可选通知
function comment_mail_notify($comment_id) {
$admin_notify = \\’1\\’; // admin 要不要收回复通知 ( \\’1\\’=要 ; \\’0\\’=不要 )
$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 : \\’\\’;
global $wpdb;
if ($wpdb->query(\\”Describe {$wpdb->comments} comment_mail_notify\\”) == \\’\\’)
$wpdb->query(\\”ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;\\”);
if (($comment_author_email != $admin_email && isset($_POST[\\’comment_mail_notify\\’])) || ($comment_author_email == $admin_email && $admin_notify == \\’1\\’))
$wpdb->query(\\”UPDATE {$wpdb->comments} SET comment_mail_notify=\\’1\\’ WHERE comment_ID=\\’$comment_id\\’\\”);
$notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : \\’0\\’;
$spam_confirmed = $comment->comment_approved;
if ($parent_id != \\’\\’ && $spam_confirmed != \\’spam\\’ && $notify == \\’1\\’) {
$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 = \\’
<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;\\”>
<p>\\’ . trim(get_comment($parent_id)->comment_author) . \\’, 您好!</p>
<p>您曾在《\\’ . get_the_title($comment->comment_post_ID) . \\’》的留言:<br />\\’
. trim(get_comment($parent_id)->comment_content) . \\\'</p>
<p>\\’ . trim($comment->comment_author) . \\’ 给您的回复:<br />\\’
. trim($comment->comment_content) . \\\'<br /></p>
<p>您可以点击查看回复的完整內容</p>
<p>还要再度光临 \\’ . get_option(\\’blogname\\’) . \\\'</p>
<p>(此邮件由系统自动发送,请勿回复.)</p>
</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 );
}
}
add_action(\\’comment_post\\’, \\’comment_mail_notify\\’);
function add_checkbox() {
echo \\\'<input type=\\”checkbox\\” name=\\”comment_mail_notify\\” id=\\”comment_mail_notify\\” value=\\”comment_mail_notify\\” checked=\\”checked\\” style=\\”margin-left:20px;\\” /><label for=\\”comment_mail_notify\\”>有人回复时邮件通知我</label>\\’;
}
add_action(\\’comment_form\\’, \\’add_checkbox\\’);
需要注意的是,我们需要将当前服务器环境支持MAIL函数才可以实现邮件推送。
猜你可能想看的VPS
- 云霸天下 IDC 香港双向 CIA 服务器,40M 独享带宽,CN2 回香港VPS[主机]
- Poli Systems→$11.19 月 1 核独享 4GB 内存 2虚拟空间(主机)
- 恒创科技2020 跨年盛典!香港服务器全场低至 5 折 续费即赠额外时长香港VPS[主机]
- SoYouStart - Summer Deals 独立服务器夏季促销独立服务器[U]
- illyhosting→€3.55 月 1GB 内存 20GB SSD 虚拟空间(主机)
- 共勉关于带宽 满速 专线等的实在话全球[VPS测评]
- 企鹅小屋→韩国 CN2 日本 CN2 独立服务器特价优惠 550 月 大独立服务器[U]
- CentOS7 创建并启用 RAMDISK(内存盘)全球[VPS测评]
- 企鹅小屋→550 元 月 E5-26XX 16GB 内存 600GB 硬日本VPS[主机]
- .htaccess 设置整站所有链接 301 重定向全球[VPS测评]
- NecoVM→199 元 年 128MB 内存 4GB 空间 500GB虚拟空间(主机)
- smarthost→大硬盘 VPS 每 T 硬盘$4 月 洛杉矶 拉斯维全球[VPS测评]
- 快云香港CN2 GIA 弹性云1G/20M双向带宽月付仅27元,2G/2香港VPS[主机]
- macOS上好用的SSH终端工具ServerCat,实时监控VPS服务器全球[VPS测评]
- KAYIZER → 1.8$ 月 土耳其 2C2G30G硬盘 信用卡支付全球[VPS测评]
- GigsGigsCloud新加坡大陆优化VPS上架全球[VPS测评]
- CloudCone 512M 内存 10G SSD 512G流量 洛杉矶全球[VPS测评]
- 搬瓦工9929 荷兰 VPS → 超稳定省心 → 1G 20G硬盘 1T全球[VPS测评]
- 加拿大以“国安”为由禁止华为、中兴参与5G建设全球[VPS测评]
- 好朋友51WORLD启动“地球克隆计划5”,我要去元宇宙参加了全球[VPS测评]
- 又一跨境电商产业园落户!中山石岐签约引进16个项目全球[VPS测评]
- 无忧云怎么样?大连BGP德阳100G高防香港CN2VPS云服务器香港VPS[主机]
- 再“掷”53亿元 宜家能否挽回中国消费者全球[VPS测评]
- ExCloud:1核256M存/8GB SSD空间/100GB流量/OV虚拟空间(主机)
- 景林网络:香港CN2vps,1核1G3M月付35元;美国高防vps,48美国VPS[主机]
- 青云互联:香港弹性云CN2直连线路,全场8折,免费windows系统,月WINDOWS
- 安国云:1核/1G/20G硬盘/1Mbp不限流量/香港cdia,月付18香港VPS[主机]
- DMIT:香港/洛杉矶GIA等KVM VPS,黑五DMIT优惠码活动,稳香港VPS[主机]
- 拼多多回应筹备跨境电商业务:不予置评全球[VPS测评]
- 30个美容院抖音唯美文案,看到就直接拿去发吧全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-142614.html











