禁用WP REST API

描述

Does one thing: Completely disables the WordPress REST API for visitors who are not logged into WordPress. No configuration required.

Important: This plugin completely disables the WP REST API for visitors who are NOT logged in to WordPress. So not recommended if your site needs the WP REST API for any non-logged users.

👉 The fast, simple way to prevent abuse of your site’s REST/JSON API
👉 Protects your site’s REST data from all non-logged users and bots
👉 Uses only 4KB of code, so super lightweight, fast, and effective

特色

  • 禁用访问者的REST / JSON(未登录)
  • 在HTTP响应中为所有用户禁用REST头
  • 已禁用所有用户的HTML头中的RESET链接
  • 100%即插即用,一劳永逸的解决方案

How does it work?

This plugin completely disables the WP REST API unless the user is logged into WordPress.

  • 对于登录用户,WP REST API正常工作
  • 对于已注销的用户,将禁用WP REST API

如果注销的访问者发出JSON / REST请求会发生什么? 他们只会收到一条简单的信息:

rest_login_required: REST API restricted to authenticated users.

This message may customized via the filter hook, disable_wp_rest_api_error. Check out this post for an example of how to do it.

隐私

This plugin does not collect or store any user data. It does not set any cookies, and it does not connect to any third-party locations. Thus, this plugin does not affect user privacy in any way. If anything it improves user privacy, as it protects potentially sensitive information from being displayed/accessed via REST API.

Disable WP REST API is developed and maintained by Jeff Starr, 15-year WordPress developer and book author.

支持此插件的开发

I develop and maintain this free plugin with love for the WordPress community. To show support, you can make a donation or purchase one of my books:

And/or purchase one of my premium WordPress plugins:

Links, tweets and likes also appreciated. Thank you! 🙂

安装

如何安装

  1. 将插件上传到您的博客并激活
  2. 完成! 无需进一步配置。

More info on installing WP plugins

测试中

要测试插件是否正常工作,请注销WordPress,然后在浏览器中请求https://example.com/wp-json/。 有关更多信息,请参阅常见问题解答

喜欢这个插件吗?

如果您喜欢《禁用WP REST API》,请花点时间 给予5星评级。 它有助于保持发展和支持的强大。 谢谢!

Uninstalling

To uninstall/remove the plugin, visit the Plugins screen, deactivate and delete the plugin. This plugin makes no changes to the WP database.

常见问题

为什么有人想要禁用REST API?

Technically this plugin disables REST API only for visitors who are not logged into WordPress. With that in mind, here are some good reasons why someone would want to disable REST API for non-logged users:

  • 非登录用户可能不需要REST API
  • 禁用REST API可节省服务器资源
  • 禁用REST API可以最大限度地减少潜在的攻击媒介
  • 禁用REST API可防止内容抓取和抄袭

我确定那边有其他有效理由,但你懂的:)

What is the default access-denied message?

When the user is logged in to WordPress, the normal REST API data will be displayed. When the user is not logged in, this is the default message:

{"code":"rest_login_required","message":"REST API restricted to authenticated users.","data":{"status":401}}

如何测试REST已禁用?

测试很简单:

  1. 注销WordPress
  2. 使用浏览器,请求https://example.com/wp-json/

如果看到以下消息,则REST是禁用状态:

“rest_login_required:REST API仅限于经过身份验证的用户。”

然后,如果您重新登录并对https://example.com/wp-json/发出新请求,您将看到REST正常工作。

它是否禁用了其他插件添加的REST功能?

Yes. If the user is NOT logged in, this plugin disables ALL endpoints that are registered with the WP REST API. Otherwise, if the user IS logged in, then this plugin does not block anything.

Does this work with Gutenberg/Block Editor?

Yes. It works the same regardless of which editor (Classic or Block) you are using.

How to customize the error message?

By default the plugin displays a message for unauthenticated users: “REST API restricted to authenticated users.” To customize that message to whatever you want, add the following code via functions.php or simple custom plugin:

function disable_wp_rest_api_error_custom($message) {

    return 'Customize your message here.'; // change this to whatever you want

}
add_filter('disable_wp_rest_api_error', 'disable_wp_rest_api_error_custom');

How to allow access for Contact Form 7?

As explained in this thread, the plugin Contact Form 7 requires REST API access in order for the contact form to work. To allow for this, follow this guide.

Got a question?

Send any questions or feedback via my contact form

评价

2024 年 5 月 6 日
I’ve tried many different solutions using functions.php because I did not want to install yet another plugin. I’m glad I found this one though. It’s simple, lightweight, maintains privacy, and functions with the latest version of WordPress. Thanks Jeff!
2023 年 3 月 29 日
In generel a Good security concept . But at the other end many plugin developer use the Rest API Could be done much easier with a 5 3 line htaccess rule to block only ^.*wp-json/wp/v2/(users But anyway a good solution if you have a simpel installation.
阅读所有36条评价

贡献者及开发者

「禁用WP REST API」是开源软件。 以下人员对此插件做出了贡献。

贡献者

「禁用WP REST API」插件已被翻译至 8 种本地化语言。 感谢所有译者为本插件所做的贡献。

帮助将「禁用WP REST API」翻译成简体中文。

对开发感兴趣吗?

您可以浏览代码,查看SVN仓库,或通过RSS订阅开发日志

更新日志

如果您喜欢《禁用WP REST API》,请花点时间 给予5星评级。 它有助于保持发展和支持的强大。 谢谢!

2.6.7

  • Adds GNU/GPL license information
  • Improves readme.txt documentation
  • Tests on PHP 8.4 and 8.5
  • Tests on WordPress 6.9

Full changelog @ https://plugin-planet.com/wp/changelog/disable-wp-rest-api.txt