WordPress 4.7.x版本禁用 REST API

最近WordPress频繁爆出漏洞,尤其是REST API部分,简直是漏洞百出,但是,从4.7.0版本开始,这部分功能被深度整合进入了核心模块,没办法彻底屏蔽REST API,只有通过rest_authentication_errors这个接口来限制非登陆用户,具体代码如下

<?php
	add_filter('rest_authentication_errors','disable_rest_api');
	function disable_rest_api(){
		if(!is_user_logged_in()){
			return new WP_Error('Error!', __('Unauthorized access is denied!','rest-api-error'), array('status' => rest_authorization_required_code()));
		}
	}
?>

此部分的代码需要被添加到当前主题的functions.php中的最后即可。
增加完成后,重启Apache2服务器,然后再访问https://www.mobibrw.com/wp-json/ 就可以看到错误信息了。

参考链接


发布者

《WordPress 4.7.x版本禁用 REST API》上有1条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注