wordpress自定义api(Wordpress评论回复邮件通知功能)WordPress功能 / WordPress的自定义用户通知功能详解...

wufei123 发布于 2024-06-17 阅读(3)

今天tearl研究了一下wordpress评论回复邮件提醒功能的添加,也是照着网上的教程来摸索有一些小细节要注意的这里怕以后忘记,就打算记录下来首先,需要测试你的环境支持不支持main()函数的部分支持没main()函数就可以直接代码操作实现回复邮件提醒。

不支持main()函数呢,wordpress需要安装smtp插件,架设配置使你能够发邮件,然后代码操作实现功能这点tearl刚开始还以为架设smtp就可以了完全是闭着眼搞那怎么知道你的环境支持不支持main()函数呢,这里有个方法,tearl也是找教程看到。

测试方式:在登录页故意按下“忘记密码”,收到邮件就有main()函数功能,没收到邮件,就有点失望zztearl在localhost测试是发送邮件不了的,在上线的站点测试,他给我一种发送成功的假象,但是并没有邮件。

所以都得安装smtp另外,网络文章有说法,虚拟主机全部都or大多数or基本上都不支持main()函数那下面介绍安装配置smtp的要点注意事项,这些细节我都规整出来,不然自己摸索你没注意到,很头疼像我一样。

smtp在wordpress后台插件安装搜索就可以搜索安装我用的是easy wp smtp安装——启用——settings然后把该填的填了然后tearl测试,错误网上找错误原因,发现主要是密码和端口两个问题,以QQ邮箱为例:。

邮箱帐号——填写你的发件邮箱地址SMTP服务器地址——smtp.qq.com接下来是邮箱密码,这里就要注意了,QQ邮箱不知何时改的政策,如果你在政策之后修改了QQ密码或者邮箱密码,那么这里所要填写的密码就应该是授权码!

何为授权码?请进入QQ邮箱-设置-账户-POP3/IMAP/SMTP/点击开启,然后通过手机发送短信获取授权码有的小伙伴可能本身就没有开启POP3/IMAP/SMTP/功能,所以, 这里记得开启一下端口分为两种情况,如果选择SSL加密,则端口号填写:465或587。

如果选择不用SSL加密,则端口号填写25以上,你就可以正常发送邮件了代码实现:评论邮件通知的方法:1.所有回复都发送邮件通知登陆博客后台,点击“外观”选项卡下的“编辑”选项进入主题编辑界面,在functions.php文件中的 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 = ‘‘ . 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) . ‘

您可以点击 查看回复完整內容欢迎再度光临 ‘ . get_option(‘blogname’) . ‘(此邮件由系统自动发送,请勿回复.)‘;$from = “From: “” . get_option(‘blogname’) . “” ”;

$headers = “$fromnContent-Type: text/html; charset=” . get_option(‘blog_charset’) . “n”;wp_mail( $to, $subject, $message, $headers );

//echo ‘mail to ‘, $to, ‘‘ , $subject, $message; // for testing}}add_action(‘comment_post’, ‘comment_mail_notify’);

// — END —————————————-2.让访客自己选择是否邮件通知在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 = ‘‘ . 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) . ‘

您可以点击查看回复的完整內容还要再度光临 ‘ . get_option(‘blogname’) . ‘(此邮件由系统自动发送,请勿回复.)‘;$from = “From: “” . get_option(‘blogname’) . “” ”;

$headers = “$fromnContent-Type: text/html; charset=” . get_option(‘blog_charset’) . “n”;wp_mail( $to, $subject, $message, $headers );

//echo ‘mail to ‘, $to, ‘‘ , $subject, $message; // for testing}}add_action(‘comment_post’, ‘comment_mail_notify’);

/* 自动加勾选栏 */function add_checkbox() {echo ‘有人回复时邮件通知我’;}add_action(‘comment_form’, ‘add_checkbox’);3.让博客管理员决定什么情况下发邮件

在functions.php文件中的 function comment_mail_notify($comment_id) {$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 : ”;

$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) && ($comment_author_email == $admin_email)) {

/* 上面的判断式,决定发出邮件的必要条件:($parent_id != ”) && ($spam_confirmed != ‘spam’): 回复的, 而且不是 spam 才可发, 必需!!($to != $admin_email) : 不发给 admin.

($comment_author_email == $admin_email) : 只有 admin 的回复才可发.可视个人需修改上面的条件.*/$wp_email = ‘no-reply@’ . preg_replace(‘#^www.#’, ”, strtolower($_SERVER[‘SERVER_NAME’])); // e-mail 发出点, no-reply 可改为可用的 e-mail.

$subject = ‘您在 [‘ . get_option(“blogname”) . ‘] 的留言有了回复’;$message = ‘‘ . 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) . ‘您可以点击 查看回复的完整內容还要再度光临 ‘ . get_option(‘blogname’) . ‘

(此邮件由系统自动发送,请勿回复.)‘;$from = “From: “” . get_option(‘blogname’) . “” ”;$headers = “$fromnContent-Type: text/html; charset=” . get_option(‘blog_charset’) . “n”;

wp_mail( $to, $subject, $message, $headers );//echo ‘mail to ‘, $to, ‘‘ , $subject, $message; // for testing

}}add_action(‘comment_post’, ‘comment_mail_notify’);// — END —————————————-4.未试过add_action(‘comment_post’,’CommentsReplyNotification’);

function CommentsReplyNotification($comment_id){//取得插入评论的id$c = get_comment($comment_id);//取得评论的父级id$comment_parent = $c->comment_parent;

//取得评论的内容$c_content = $c->comment_content;//评论者email$c_author_email = $c->comment_author_email;if($comment_parent != 0){

$pc = get_comment($comment_parent);$comment_ID = $pc->comment_ID;$comment_author = $pc->comment_author;

$comment_author_email = $pc->comment_author_email;$comment_post_ID = $pc->comment_post_ID;$comment_content = $pc->comment_content;

$ps = get_post($comment_post_ID);$author_id = $ps->post_author;$u_email = get_user_meta($author_id,’email’,true);

//判断自己的回复,如果自己参与评论,不给自己发送邮件通知if($c_author_email == $comment_author_email || $comment_author_email == $u_email ){

return;}$post_title = $ps->post_title;$link = get_permalink($comment_post_ID);//邮件内容,可以自定义内容$content = “尊敬的”.$comment_author.”您好,你发布于” “.$post_title.””的评论:rn”.$comment_content.”rn有了回复:rn”.$c_content.”rn点击链接回复评论:”.$link.”#comment-“.$comment_ID;

//发送邮件wp_mail($comment_author_email,’评论回复:’.$post_title, $content);}}5.评论必须经过审核才会发送通知邮件function logcg_comment_mail_notify($comment_id, $comment_status) {

// 评论必须经过审核才会发送通知邮件if ($comment_status !== ‘approve’ && $comment_status !== 1)return;$comment = get_comment($comment_id);

if ($comment->comment_parent != ‘0’) {$parent_comment = get_comment($comment->comment_parent);// 邮件接收者email

$to = trim($parent_comment->comment_author_email);// 邮件标题$subject = ‘您在[‘ . get_option(“blogname”) . ‘]的留言有了新的回复’;

// 邮件内容,自行修改,支持HTML$message = ‘您在 ‘ . get_option(‘blogname’) . ‘的留言有了新回复!‘ . $parent_comment->comment_author . ‘,您好!

您曾在 [‘ . get_option(“blogname”) . ‘] 的文章《’ . get_the_title($comment->comment_post_ID) . ‘》 上发表评论:‘ . nl2br($parent_comment->comment_content) . ‘

‘ . trim($comment->comment_author) . ‘ 给您的回复如下:‘ . nl2br($comment->comment_content) . ‘您也可移步 tearl.club 以查看回复的完整內容。

欢迎再次光临 ‘ . get_option(‘blogname’) . ‘(此邮件由系统自动发出, 请勿回复)‘;$message_headers = “Content-Type: text/html; charset=””.get_option(‘blog_charset’).””n”;。

// 不用给不填email的评论者和管理员发提醒邮件if($to != ” && $to != get_bloginfo(‘admin_email’))@wp_mail($to, $subject, $message, $message_headers);

}}// 编辑和管理员的回复直接发送提醒邮件,因为编辑和管理员的评论不需要审核add_action(‘comment_post’, ‘logcg_comment_mail_notify’, 20, 2);

// 普通访客发表的评论,等博主审核后再发送提醒邮件add_action(‘wp_set_comment_status’, ‘logcg_comment_mail_notify’, 20, 2);老样子,任何的问题,请留言。

好了,根据你可以根据你使用的需要选择代码以上,注意点做好了按流程就能像tearl一样实现wordpress评论回复邮件提醒功能了另感谢落格博客、wordpress大学、晓兔博客的互利网资料共享——踢儿l解忧杂货店。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

河南中青旅行社综合资讯 奇遇综合资讯 盛世蓟州综合资讯 综合资讯 游戏百科综合资讯 新闻80290