当你在评论博主文章,博主给了你回复,此时系统会自动发出一份邮件给评论者,这样有利于博主与评论者之间的互动。
这个功能是威廉大师写的,非常不错,是Wordpress不可缺少的代码。
将一下代码放至模板函数(functions.php)中
<code>
<?php
if (function_exists('register_nav_menus')){
register_nav_menus( array(
'footmenubar' => __('TAG Menubar', 'TAG MENU')
) );
}
/* 开始*/
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:#fff; border:1px solid #666666; color:#111;border-radius:8px; font-size:13px; width:702px; margin:0 auto; margin-top:10px; font-family:微软雅黑, Arial;">
<div style="background:#666666; width:100%; height:60px; color:white; border-radius:6px 6px 0 0; "><span style="height:60px; line-height:60px; margin-left:30px; font-size:20px;">您在<a style="text-decoration:none; color:#00bbff;font-weight:600;" href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a>博客上的留言有回复啦!</span></div>
<div style="width:90%; margin:0 auto">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 10px 5px;margin: 15px 0;">'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回复:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding:10px 5px;margin: 15px 0;">
'. trim($comment->comment_content) . '</p>
<p>您可以点击 <a style="text-decoration:none; color:#00bbff" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复的完整內容</a></p>
<p>欢迎再次光临 <a style="text-decoration:none; color:#00bbff" href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p style="color:red;">(温馨提示:本邮件由系统自动发出,切勿回复)</p>
</div>
</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, '<br/> ' , $subject, $message; // for testing
}
}
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');
?>
</code>
<?php
if (function_exists('register_nav_menus')){
register_nav_menus( array(
'footmenubar' => __('TAG Menubar', 'TAG MENU')
) );
}
/* 开始*/
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:#fff; border:1px solid #666666; color:#111;border-radius:8px; font-size:13px; width:702px; margin:0 auto; margin-top:10px; font-family:微软雅黑, Arial;">
<div style="background:#666666; width:100%; height:60px; color:white; border-radius:6px 6px 0 0; "><span style="height:60px; line-height:60px; margin-left:30px; font-size:20px;">您在<a style="text-decoration:none; color:#00bbff;font-weight:600;" href="' . get_option('home') . '" target="_blank">' . get_option('blogname') . '</a>博客上的留言有回复啦!</span></div>
<div style="width:90%; margin:0 auto">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 10px 5px;margin: 15px 0;">'
. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 给您的回复:</p>
<p style="background-color: #EEE;border: 1px solid #DDD;padding:10px 5px;margin: 15px 0;">
'. trim($comment->comment_content) . '</p>
<p>您可以点击 <a style="text-decoration:none; color:#00bbff" href="' . htmlspecialchars(get_comment_link($parent_id)) . '">查看回复的完整內容</a></p>
<p>欢迎再次光临 <a style="text-decoration:none; color:#00bbff" href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p style="color:red;">(温馨提示:本邮件由系统自动发出,切勿回复)</p>
</div>
</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, '<br/> ' , $subject, $message; // for testing
}
}
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');
?>
</code>
如果不想在评论框下面显示
有人回复时邮件通知我
你可以将代码中的
/* 自动加勾选栏 */
一下的内容用/* */注释起来。