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
- hkserversolution→cn2 高防 VPS 99 元 月 K全球[VPS测评]
- 超优惠 磐石云元旦特惠→1 核 2G 内存 50G 硬盘 2G 防御 三全球[VPS测评]
- 价格高-Aurora→日本 VPS 1 核 512M 内存 10G SS日本VPS[主机]
- 一个五一个十是什么成语?全球[VPS测评]
- 号外 Virmach 洛杉矶机房 107.175.x.x 段 IP 可以全球[VPS测评]
- Laravel 报错 Call to undefined functio全球[VPS测评]
- PacificRack→$13.5 年 KVM-1GB 35GB 1TB全球[VPS测评]
- CloudCone→域名邮箱服务 10GB 空间 每月可发 1000 封虚拟空间(主机)
- 2019 年 Virmach 低价 VPS 购买及优惠码使用教程全解全球[VPS测评]
- 疯狂猜成语 图猜成语很多亲戚关系的字被关在笼子里是什么成语?全球[VPS测评]
- WordPress插件:WP User Avatar修改默认头像全球[VPS测评]
- 用Memcached、Batcache加速WordPress实现内存级缓全球[VPS测评]
- Phanes Cloud → 黑五 2.49€ 月 荷兰 1C1G30G全球[VPS测评]
- Mtbone → 泰国VPS 300THB 月 1核1G20G硬盘 无限全球[VPS测评]
- Virmach 黑五及超级星期一活动全球[VPS测评]
- LightCloud 2019 新春: 终身折扣 VDS折扣 充全球[VPS测评]
- OLVPS 512 内存 10G SSD 512G流量 1Gbps 俄罗全球[VPS测评]
- 香港云服务器的优势有哪些方面香港VPS[主机]
- CMCUCT:2核2GB/20GB香港ntt线路vps 6TB流量/1G香港VPS[主机]
- 云服务器能干什么?有几十种玩法?阿里云,腾讯云服务器如何购买?全球[VPS测评]
- 八点云主机怎么样?香港2核4G5M带宽云主机,98元/月;国内4核8G云香港VPS[主机]
- 中概股扫描网传拼多多正筹备跨境电商平台首站登陆美国美国VPS[主机]
- 中国跨境电商,如何掘金欧洲市场?全球[VPS测评]
- 阿里云服务器一年价格多少钱?阿里云服务器报价全球[VPS测评]
- 快速云:vps云服务器的区别是什么?2022-08-1913:32来源:全球[VPS测评]
- Rustrot-深绿色UI电商网站模板HTML5框架全球[VPS测评]
- 柚子互联:美国洛杉矶CN2 vps,防御100G/带宽10M,28元/月美国VPS[主机]
- PQhosting:香港HE/乌克兰/俄罗斯/荷兰/摩尔多瓦vps主机/香港VPS[主机]
- 汇速云:香港沙田CN2云主机,2核CPU带宽1M月付6元,2核2M带宽月香港VPS[主机]
- 云米科技:新春欢庆,香港云服务器/美国高防vps服务器全场八折,支持按天美国VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-39613.html











