首页 » 网站建设 » themeoptionsphp技巧_WordPress简单实现评论邮箱白名单功能教程

themeoptionsphp技巧_WordPress简单实现评论邮箱白名单功能教程

访客 2024-11-19 0

扫一扫用手机浏览

文章目录 [+]

一、白名单列表

在 functions.php 文件扔进以下代码:

themeoptionsphp技巧_WordPress简单实现评论邮箱白名单功能教程

/

themeoptionsphp技巧_WordPress简单实现评论邮箱白名单功能教程
(图片来自网络侵删)

定义白名单

龙砚庭 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导航

标签:

相关文章

微信第三方登录便捷与安全的完美融合

社交平台已成为人们日常生活中不可或缺的一部分。微信作为我国最受欢迎的社交软件之一,拥有庞大的用户群体。为了方便用户在不同平台间切换...

网站建设 2025-02-18 阅读1 评论0

广东高速代码表解码高速公路管理智慧

高速公路作为国家交通动脉,连接着城市与城市,承载着巨大的物流和人流。广东作为我国经济大省,高速公路网络密布,交通流量巨大。为了更好...

网站建设 2025-02-18 阅读1 评论0