WordPress評論回復郵件提醒-簡單模板
編輯:狂族晨曦 來源:建站分享,WordPress技巧 日期:2015-08-28 閱讀: 3,091 次 搶個沙發 百度已收錄
最后更新時間:2015-10-10
博客網站有很多,有時候別人在你的網站上評論后,可能很快就會忘了,所以當有人評論回復是郵件提醒對方,就顯得很有用了。很多博客都有評論回復的功能,要實現這個功能也很簡單,下面就提供一個相對簡單的模板。
首先是看效果圖:

實現代碼
要實現這個功能,將以下的代碼,添加在主題的 functions.php 文件的 最后一個 ?> 前面即可:
/**
* WordPress 評論回復郵件通知
* //www.cnidcc.cn
* DIY By 成都航院計算機工程系
**/
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() 函數,如果你發現沒辦法收到郵件,可以詢問你的主機商。由于每個人的主機環境不一樣,有些朋友在添加這個功能的時候,總是不能成功,這時候,你可以試試 SMTP 發送郵件的方式。
本文參考自:http://www.wpdaxue.com/comment-mail-notify.html,由狂族晨曦(capjsj.cn)整理編輯,本文共3076個字。
本文鏈接地址:http://www.cnidcc.cn/wordpress_plhfyjtx_jdmb.html , 轉載請保留本說明!
本文鏈接地址:http://www.cnidcc.cn/wordpress_plhfyjtx_jdmb.html , 轉載請保留本說明!
相關文章
已有 0 位"計工"發布了激烈的評論,還有N多人圍觀笑而不語評論

川公網安備 51011202000104號