WordPress添加评论回复邮件通知
正如题目所言,Wordpress的评论是其一大重要功能,如果用户在收到了评论回复时能够及时收到提醒,这对于提升用户体验与保持网站热度来说是尤为重要的。
那么,现在,就让我教给您如何给Wordpress站点添加评论回复邮件通知功能吧~
下面提供两种方式
方案一:所有被回复的评论都发送邮件通知
请将下面的代码添加到主题模板functions.php中的适当位置(大约在倒数第二行附近即可)
/* comment_mail_notify v1.0 by willin kan. (所有回复都发邮件) */
function comment_mail_notify($comment_id) {
$comment = get_comment($comment_id);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam')) {
$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');
// -- END ----------------------------------------
方案二: 在评论框下方显示一个勾选框,让评论人自己决定是否接收邮件通知
请将下面的代码添加到主题模板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>您可以点击查看回复的完整內容</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');
注意:
1.Ajax加载评论的主题方案二不能直接使用
2.进行操作前请确保您正确配置了SMTP服务器
3.代码中的提示内容可自行修改
代码不能复制?不要慌,下面有源码
客户端用户请调出左边栏,通过浏览器访问
https://www.syf.ink/cloud/index.php?share/file&user=1&sid=uaIfcVBb
除非注明,文章均为学霸时光机原创,转载请遵循下文,注明出处URL
本站文章许可遵循《“知识共享”国际许可协议4.0》
本文地址:https://www.syf.ink/archives/755
评论请前往留言板,为您推荐以下文章~
评论 (4)
You actually make it appear really easy with your presentation but I to
find this topic to be actually one thing which I think I would
by no means understand. It seems too complex and extremely broad
for me. I am having a look ahead to your next put
up, I’ll attempt to get the cling of it!
Hi! I understand this is sort of off-topic but I had to ask.
Does managing a well-established website such as yours
take a lot of work? I’m brand new to running a blog but I do write in my journal daily.
I’d like to start a blog so I can share my own experience and feelings online.
Please let me know if you have any kind of ideas or tips for new
aspiring bloggers. Thankyou!
@hcg injections in Belmont: I am happy to see your comment, you can send email to vip@syf.ink. I am glad to talk about it with you.
有些主题自带简直省心