虚位以待(AD)
虚位以待(AD)
首页 > CMS教程 > WordPress > WordPress评论邮件通知无插件实现思路及代码

WordPress评论邮件通知无插件实现思路及代码
类别:WordPress   作者:码皇   来源:互联网   点击:

通常我们喜欢使用Wordpress Thread Comment 或 Mail To Commenter这两款插件前者久未更新,已测支持WordPress2 9 2版本 其他的未测试,优点,不进垃圾箱,支持嵌套回复。 后者据网友反馈 该插件容易造成邮件延迟 邮件 ,魔客吧
通常我们喜欢使用Wordpress Thread Comment 或 Mail To Commenter这两款插件前者久未更新,已测支持WordPress2.9.2版本 其他的未测试,优点,不进垃圾箱,支持嵌套回复。
后者据网友反馈 该插件容易造成邮件延迟 邮件进垃圾箱或收不到邮件的情况。
今天来通过无插件实现WordPress评论邮件通知,先看效果:
方法:在主题目录的functions中增加以下代码:

复制代码 代码如下:
/* comment_mail_notify v1.0 by willin kan. (無勾選欄) */
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 = '

Hi !' . 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') . "" <$wp_email>";
$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 ----------------------------------------

已测试 兼容WordPress3.1版本及多款主题。
相关热词搜索: WordPress评论邮件通知无插件实现思路及代