一、白名单列表
在 functions.php 文件扔进以下代码:
/

定义白名单
龙砚庭 https://loomob.com
/
function lyt_white_list_check($email) {//用邮箱作为判断依据
$white_list = array_filter( explode( ',', trim(get_option('lyt_white_list'), ',' ) ) );//请根据自用主题的后台选项修正 get_option
if ( !empty($white_list) && in_array($email, $white_list) )
return true;
return false;
}
这里有两个地方须要把稳,一是$email,二是 get_option。请根据自用主题的实际情形来修正,这里不做细表。
二、实行机制
同样还是扔到 functions..php 文件中。
/
白名单内指定 url 可评论,且指定某页面与文章过滤此规则。
龙砚庭 https://loomob.com
/
function white_list_spam($comment) {
$postsid = get_option('lyt_whitelist_id'); //自定义过滤的页面、文章 ID
define('ALLOW_PAGES', ''.$postsid.'');
$post_ID = $comment[\公众comment_post_ID\"大众];
$allow_posts = ALLOW_PAGES ? explode(',', ALLOW_PAGES) : array();
if(!in_array($post_ID,$allow_posts)){
if (!lyt_white_list_check($comment['comment_author_email'])) {//通过邮箱判断
err(__('对不起,您的网站不在龙砚庭博客的评论白名单之内,请邮件联系站长'));//err 或者 wp-die
}
}
return $comment;
}
add_filter('preprocess_comment','white_list_spam');
三、后台选项函数
为了可视化,便于后台直接操作,我们还须要给后台主题选项写一个白名单列表函数,以及过滤页面、文章 ID 的函数
lyt_white_list
lyt_whitelist_id
如果你的主题后台选项框架利用的是类似轻量级的 Git 主题主题框架的,那么请直接采取以下代码,放到 theme-options.php 里面得当的位置。
array(
'name' => '评论白名单列表',
'desc' => '请输入邮箱,并以英文逗号隔开',
'id' => 'lyt_white_list',
'type' => 'textarea',
),
array(
'name' => '评论白名单过滤 ID',
'desc' => '请输要过滤的页面、文章 ID,并以英文逗号隔开',
'id' => 'lyt_whitelist_id',
'type' => 'textarea',
),
教程到此结束,剩下的便是在日常运维的时候,逐步扩充白名单列表。当然,以上白名单列表的实现办法是目前我以为最大略、便捷的代码,至于更繁芜的代码我就不贴了,由于能够实现功能就行,不信你试试!
作者:龙砚庭博客
来源:boke112导航