WordPress留言评论自动邮件通知功能实现方法

WordPress默认主题中用户留言评论,如果我们去回复之后,或者我们用户评论的时候也不会自动通知博主的。我们可以添加评论邮件通知,这样可以及时的与用户进行互动。当然,有很多插件可以实现这样的功能,在这篇文章中,本站收集到的办法是不用插件,直接在当前主题Functions.php文件中加入代码就可以实现。

第一、自动默认全部通知

// 评论回应邮件通知 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 = \’

\’ . 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\’) . \”\\\” \”; $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\’); // 自动勾选 function add_checkbox() { echo \'有人回复时邮件通知我\’; } add_action(\’comment_form\’, \’add_checkbox\’);

第二、用户可选通知

/* 用户选择是否通知*/ 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 = \’

\’ . 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\’) . \”\\\” \”; $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 \'有人回复时邮件通知我\’; } add_action(\’comment_form\’, \’add_checkbox\’);

需要注意的是,我们需要将当前主机环境支持MAIL函数才可以实现邮件推送。

推荐站内搜索:美国仿牌空间、中国代理ip、日本代理服务器ip、cc域名、便宜域名注册、美国主机网、域名如何备案、虚拟主机、100m免费空间美国独立主机、