WordPress評論郵件通知無插件實(shí)現(xiàn)思路及代碼
發(fā)布時(shí)間:2013-01-14 09:50:38 作者:佚名
我要評論

通常我們喜歡使用Wordpress Thread Comment 或 Mail To Commenter這兩款插件前者久未更新,已測支持WordPress2.9.2版本 其他的未測試,優(yōu)點(diǎn),不進(jìn)垃圾箱,支持嵌套回復(fù),今天來通過無插件實(shí)現(xiàn)WordPress評論郵件通知
通常我們喜歡使用Wordpress Thread Comment 或 Mail To Commenter這兩款插件前者久未更新,已測支持WordPress2.9.2版本 其他的未測試,優(yōu)點(diǎn),不進(jìn)垃圾箱,支持嵌套回復(fù)。
后者據(jù)網(wǎng)友反饋 該插件容易造成郵件延遲 郵件進(jìn)垃圾箱或收不到郵件的情況。
今天來通過無插件實(shí)現(xiàn)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)) {
/* 上面的判斷式,決定發(fā)出郵件的必要條件:
($parent_id != '') && ($spam_confirmed != 'spam'): 回覆的, 而且不是 spam 才可發(fā), 必需!!
($to != $admin_email) : 不發(fā)給 admin.
($comment_author_email == $admin_email) : 只有 admin 的回覆才可發(fā).
可視個(gè)人需求修改以上條件.
*/
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發(fā)出點(diǎn), no-reply 可改為可用的 e-mail.
$subject = '您在【' . get_option("blogname") . '】的留言有回復(fù)了!去看看吧 ^_^';
$message = '
<div style="margin: 1em 40px 1em 40px;background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding: 0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;border-radius:5px;">
<p>Hi !<strong>' . trim(get_comment($parent_id)->comment_author) . '</strong>,您好!
您曾在<strong>《' . get_the_title($comment->comment_post_ID) . '》</strong>的留言有了新回復(fù)。</p></div>
<div style="margin: 1em 40px 1em 40px;background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding: 0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;border-radius:5px;"><p><strong>
您</strong>說:' . trim(get_comment($parent_id)->comment_content) . '</p>
<p><strong>' . trim($comment->comment_author) . '</strong>說:
' . trim($comment->comment_content) . '</p>
<p>您可以點(diǎn)擊<a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回復(fù)內(nèi)容。</a></p>
<p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(此郵件由系統(tǒng)自動發(fā)送,請勿回復(fù)。)</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, '
' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------
已測試 兼容WordPress3.1版本及多款主題。
后者據(jù)網(wǎng)友反饋 該插件容易造成郵件延遲 郵件進(jìn)垃圾箱或收不到郵件的情況。
今天來通過無插件實(shí)現(xiàn)WordPress評論郵件通知,先看效果:
方法:在主題目錄的functions中增加以下代碼:
復(fù)制代碼
代碼如下:/* 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)) {
/* 上面的判斷式,決定發(fā)出郵件的必要條件:
($parent_id != '') && ($spam_confirmed != 'spam'): 回覆的, 而且不是 spam 才可發(fā), 必需!!
($to != $admin_email) : 不發(fā)給 admin.
($comment_author_email == $admin_email) : 只有 admin 的回覆才可發(fā).
可視個(gè)人需求修改以上條件.
*/
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 發(fā)出點(diǎn), no-reply 可改為可用的 e-mail.
$subject = '您在【' . get_option("blogname") . '】的留言有回復(fù)了!去看看吧 ^_^';
$message = '
<div style="margin: 1em 40px 1em 40px;background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding: 0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;border-radius:5px;">
<p>Hi !<strong>' . trim(get_comment($parent_id)->comment_author) . '</strong>,您好!
您曾在<strong>《' . get_the_title($comment->comment_post_ID) . '》</strong>的留言有了新回復(fù)。</p></div>
<div style="margin: 1em 40px 1em 40px;background-color:#eef2fa;border:1px solid #d8e3e8;color:#111;padding: 0 15px;-moz-border-radius:5px;-webkit-border-radius:5px;-khtml-border-radius:5px;border-radius:5px;"><p><strong>
您</strong>說:' . trim(get_comment($parent_id)->comment_content) . '</p>
<p><strong>' . trim($comment->comment_author) . '</strong>說:
' . trim($comment->comment_content) . '</p>
<p>您可以點(diǎn)擊<a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看完整的回復(fù)內(nèi)容。</a></p>
<p>歡迎再度光臨 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(此郵件由系統(tǒng)自動發(fā)送,請勿回復(fù)。)</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, '
' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');
// -- END ----------------------------------------
已測試 兼容WordPress3.1版本及多款主題。
相關(guān)文章
CyberPanel安裝WordPress并配置偽靜態(tài)規(guī)則
下面教你如何在 CyberPanel安裝WordPress以及配置偽靜態(tài),需要的朋友可以參考下2023-12-27- 這篇文章主要介紹了wordpress無法安裝更新主題插件的解決辦法,需要的朋友可以參考下2020-12-27
WordPress必備數(shù)據(jù)庫SQL查詢語句整理
發(fā)現(xiàn)幾條比較實(shí)用的,適合 WordPress 實(shí)用的SQL語句。于是就趕緊收集分享出來了,需要的朋友可以參考下2017-09-23wordpress在安裝使用中出現(xiàn)404、403、500及502問題的分析與解決方法
wordpress是很多新手站長搭建個(gè)人博客最喜愛的程序,但是最近在使用WordPress的時(shí)候遇到了一些問題,所以想著將遇到問題總結(jié)分享出來,下面這篇文章主要給大家介紹了關(guān)于wo2017-08-11WordPress取消英文標(biāo)點(diǎn)符號自動替換中文標(biāo)點(diǎn)符號的優(yōu)雅方法
這篇文章主要介紹了WordPress取消英文標(biāo)點(diǎn)符號自動替換中文標(biāo)點(diǎn)符號的優(yōu)雅方法,需要的朋友可以參考下2017-04-04- 這篇文章主要給大家介紹了wordpress自定義上傳文件類型的方法,如WordPress默認(rèn)允許上傳 .exe 后綴名的可運(yùn)行文件,那么我們怎么禁止用戶在WordPress后臺發(fā)表文章時(shí)上傳 .e2016-12-19
- 大家可能發(fā)現(xiàn)了當(dāng)實(shí)現(xiàn)了前端用戶中心,后臺控制面板就失去了作用,那么限制其他用戶進(jìn)入后臺控制面板就很有必要了!那么我們要怎么做呢?通過下面這篇文章分享的方法后,只2016-12-19
WordPress實(shí)現(xiàn)回復(fù)文章評論后發(fā)送郵件通知的功能
這篇文章主要介紹了WordPress實(shí)現(xiàn)回復(fù)文章評論后發(fā)送郵件通知的功能,涉及wordpress針對評論與郵件的相關(guān)操作技巧,需要的朋友可以參考下2016-10-11WordPress使用自定義文章類型實(shí)現(xiàn)任意模板的方法
這篇文章主要介紹了WordPress使用自定義文章類型實(shí)現(xiàn)任意模板的方法,可通過自定義文章類型來實(shí)現(xiàn)任意模版的使用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-10-11WordPress后臺地址被改導(dǎo)致無法登陸后臺的簡單解決方法
這篇文章主要介紹了WordPress后臺地址被改導(dǎo)致無法登陸后臺的簡單解決方法,簡單分析了后臺無法登陸的原因與相應(yīng)的解決方法,涉及針對wordpress配置項(xiàng)的簡單修改,需要的朋友2016-10-11