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
- OVZ 不推荐-Webtropia→€1.5 月 2GB 内存 100G全球[VPS测评]
- OneVPS→日本东京完全可看美区 Netflix 耐飞视频 VPS N日本VPS[主机]
- 口字上面有一个病字是什么成语?全球[VPS测评]
- 高防服务器 L5630*2 16G 内存 480G SSD 50M 独享全球[VPS测评]
- 闲话撸 10 京东卡全球[VPS测评]
- DediPath→VPS 全场 5 折 Hybrid Servers 全全球[VPS测评]
- 四折优惠 $0.8 月 256M 内存 15G 硬盘 256G 流量 1全球[VPS测评]
- Kuai Che Dao→$18.3 季 512MB 内存 10GB S虚拟空间(主机)
- 智商税-新睿云免费云服务器→国内 4 核 8G 5M 带宽云服务器免费一全球[VPS测评]
- veesp→$1 月 KVM-512MB 10GB 100GB 圣彼得堡全球[VPS测评]
- STSDUST→$25 月 2 核 2GB 内存 20GB 空间 10T虚拟空间(主机)
- CN2 ¥69 月 2 核 CPU 1G 内存 50G 硬盘 2Mbps香港VPS[主机]
- 一个击字一个声字是什么成语?全球[VPS测评]
- STSDUST→$51.48 年 1GB 内存 20GB SSD 空间 虚拟空间(主机)
- HostUS→$6.45 月 4 核 1GB 内存 50GB SSD 空虚拟空间(主机)
- 阿里国际→香港虚拟主机 5GB 空间 512MB 内存 50GB 月流量虚拟空间(主机)
- 便宜 $2 月 512M 内存 10G SSD 1Gbps 不限流量 K全球[VPS测评]
- 华为云 开年采购季,新用户享 1 核 2G 云服务器 79 元 年,消费全球[VPS测评]
- AcroServers 波兰商家,全球38个节点,默认1Gbps带宽,€全球[VPS测评]
- Wikihost → 韩国BGP CN2 → 1G 100G SSD韩国VPS[主机]
- HKServerSolution 2G内存 20G HDD 2T流量 1日本VPS[主机]
- Centos7的firewall 防火墙如何设置端口转发?全球[VPS测评]
- 人力资源管理系统前端Web模板 - SmartHR全球[VPS测评]
- 拼多多正筹建跨境电商平台:密集挖角SHEIN员工,零佣金招商入驻全球[VPS测评]
- centos7如何查看firewall防火墙状态:开启/关闭/重启防火墙全球[VPS测评]
- 人们为啥不爱吃海底捞了?海底捞上半年亏损达2.97亿 网友神评扎心全球[VPS测评]
- 纳米云怎么样?纳米云香港vps,4核1G主机价格80元/月香港VPS[主机]
- 跨境电商周报丨拼多多布局跨境电商平台;Shopify将推出新平台支持网红全球[VPS测评]
- 易科云:香港cn2 vps,洛杉矶 cn2云主机;全场月付9折/季付8折香港VPS[主机]
- 印象云vps,香港安畅gia低至22元起,1核1G内存,小带宽无限流量,香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-39613.html











