wordpress如何屏蔽wp-json(禁用REST API)

wordpress如何屏蔽wp-json(禁用REST API)

ruinous
2020-09-27 / 0 评论 / 46 阅读 / 正在检测是否收录...

通过REST API可以很轻松的获取网站的数据,但是这个功能并不是每个网站都需要的,或者说有需要但并不希望它在head里面输出,那么可以禁用REST API或者说移除head里面wp-json链接。

// 屏蔽 REST API
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');

// 移除头部 wp-json 标签和 HTTP header 中的 link 
remove_action('wp_head', 'rest_output_link_wp_head', 10 );
remove_action('template_redirect', 'rest_output_link_header', 11 );

另外需要注意的是,屏蔽 REST API 会直接导致 文章 Emebed 功能失效。

1、使用非插件方式解决:

将下面代码添加到主题functions.php文件中,去除head里面输出的链接信息。

//屏蔽 REST API
add_filter('json_enabled', '__return_false' );
add_filter('json_jsonp_enabled', '__return_false' );
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');

// 移除头部 wp-json 标签和 HTTP header 中的 link
remove_action('wp_head', 'rest_output_link_wp_head', 10 );
remove_action('template_redirect', 'rest_output_link_header', 11 );

2、使用disable-json-api插件方式解决:

1、在插件安装哪里搜索 disable-json-api ,安装启动即可;

2、手动下载复制到插件安装目录,启动即可,下载地址:https://wordpress.org/plugins/disable-json-api/

0

评论

博主关闭了所有页面的评论