因为,WordPress 实现的原因,已经无法简单地把 REST API 一禁了之。
如何防止爬虫通过 REST API 爬取文章呢?
给 REST API 加上登录验证就可以了。具体怎么办呢?答案是 rest_authentication_errors 钩子。
add_filter( 'rest_authentication_errors', function( $result ) {
// If a previous authentication check was applied,
// pass that result along without modification.
if ( true === $result || is_wp_error( $result ) ) {
return $result;
}
// No authentication has been performed yet.
// Return an error if user is not logged in.
if ( ! is_user_logged_in() ) {
return new WP_Error(
'rest_not_logged_in',
__( 'You are not currently logged in.' ),
array( 'status' => 401 )
);
}
// Our custom authentication check should have no effect
// on logged-in requests
return $result;
});
返回 true 表示验证通过,false 表示验证失败。返回 WP_Error 表示验证失败的信息。
-
WordPress圈子到底怎么了...话说,国内WordPress圈子是否还有新的发展机遇?这个充斥着同质化产品和激烈内卷的市场中,如何在价格和功能的差异化上寻求突破,为圈子注入新的活力,是开发者不得不面对的问题。
-
重塑WordPress生态的思考与探索前两天的《WordPress圈子到底怎么了...》这篇文章大家都挺有感触的,群里聊得热火朝天。里头啥声音都有:有的哥们儿直接吐槽WordPress性能拖后腿,躲都躲不及;有的则感慨流量难搞,但坚持建站纯粹是出于热爱和那份情怀;还有人预言,以后建站啊,模块化、可视化才是王道;较热闹的还得数短视频和直播,这家伙一来,内容消费整个儿变了天,网站和小程序怎么赚钱都成难题了。
-
Loco Translate 一款专为WordPress设计的翻译插件WordPress Loco Translate是一款专为WordPress用户设计的翻译插件,它简化了主题和插件的多语言翻译过程。
-
WordPress后台待审文章显示气泡提示的方法之前小编分享过一篇《WordPress 后台菜单添加 badge-红点提示(气泡通知)》的文章,今天我简单说说待审文章(为全部文章类型显示待审角标提示)的气泡提示如何操作。
-
FluentSMTP一款功能强大且免费的WordPress SMTP插件FluentSMTP是一款功能强大且免费的SMTP插件,它支持为WordPress配置多个SMTP发送服务器。
-
追格小程序V2.5.8更新发布追格小程序是一款采用积木式理念的微信小程序构建框架,它巧妙融合了Uniapp与WordPress技术,打造出一个开源且无加密限制的开发环境。
暂无评论,抢个沙发...