提供seo看见别的网站评论框之后都有“有人回复我时邮件通知我”这个选项,发现挺不错的,所以就添加了一个。
非常的简单,只需一段代码即可,当然你也可以使用插件,但是用过WordPress Thread Comment插件之后感觉占用资源很大,而且不方便,还是为网站添加SEO代码里的话,能用代码代替的就尽量不用插件。
“有人回复我时邮件通知我”代码如下,把它添加到functions.php文件的<?php到?>之间即可:
/**
有人回复我时邮件通知我代码
*/
//-- 开始 --------------------------------------
function comment_mail_notify($comment_id) {
$admin_notify = '1'; // are you willing to receive the mail? 1 is yes.
$admin_email = get_bloginfo ('admin_email'); // you can change $admin_email to your e-mail optionaly.
$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
$to = trim(get_comment($parent_id)->comment_author_email);
$subject = __('You got a reply from ','zhuti') .'--'. 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; border-radius:5px;">
<p>' . __('Hello:','zhuti') . '</p>
<p>' . trim(get_comment($parent_id)->comment_author) .__('. Your wrote a comment on -','zhuti') . get_the_title($comment->comment_post_ID) . ':<br />'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . __(' replied you','zhuti') . ':<br />'
. trim($comment->comment_content) . '<br /></p>
<p>' . __('You can ','zhuti') . '<a href="' . htmlspecialchars(get_comment_link($parent_id)) . '">' . __('Click here to see the whole comments.','zhuti') . '</a>
<a href='.htmlspecialchars(get_comment_link($parent_id)).'>'.htmlspecialchars(get_comment_link($parent_id)).'</a></p>
<p> <a href="' . home_url() . '">' . get_option('blogname') . '</a></p>
<p> '. __('(System email, Do Not Reply.)','zhuti').'</p>
</div>';
$from = "From: " . get_option('blogname') . " <$wp_email>";
$headers = "$fromnContent-Type: text/html; charset=" . get_option('blog_charset');
wp_mail( $to, $subject, $message, $headers );
//echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
}
}
add_action('comment_post', 'comment_mail_notify');/* Auto checked */
function add_checkbox() {
echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" /><label for="comment_mail_notify">有人回复我时邮件通知我</label>';
}
add_action('comment_form', 'add_checkbox');// -- 结束 ----------------------------------------
添加之后,上传覆盖你的原文件,打开网站文章之后评论框下面就会显示,提供网站优化分享,赶快试试吧。