Disable 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 標頭中的 REST 連結
  • 完全隨裝即用,設定後不需後續管理

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.

這個訊息可透過篩選器勾點 disable_wp_rest_api_error 進行自訂,請參考這篇文章的範例了解如何使用這個篩選器勾點。

隱私權

這個外掛不會收集或儲存任何使用者資料。它不會設定任何 Cookie,也不會連線至任何第三方位置,因此它不會透過任何方式侵犯使用者隱私。相反的,這個外掛可以改善使用者隱私,因為它可以保護潛在的敏感資訊不會透過 TEST API 顯示/存取。

Disable WP REST API 由具備 15 年 WordPress 開發者書籍作者經驗的 Jeff Starr 開發及維護。

支持這個外掛的開發工作

因為喜愛 WordPress 社群,因此我開發並維護這個免費外掛。如果想要支持外掛開發工作,請贊助開發工作或購買我撰寫的書籍:

也可以透過購買以下付費 WordPress 外掛支持外掛開發:

十分歡迎外部連結、轉推及按讚,謝謝大家 🙂

安裝方式

手動安裝

  1. 上傳外掛檔案至網站並完成啟用。
  2. 大功告成!不需進行任何組態。

More info on installing WP plugins

測試方式

如需了解這個外掛是否正確執行,請登出 WordPress 並在瀏覽器中對 https://example.com/wp-json/ 提出要求。請參考〈常見問題集〉以進一步了解。

如果這個外掛對網站有所幫助

如果 Disable 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.

常見問題集

為什麼會有停用 RSET 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 可以防止內容遭到擷取及抄襲

這裡還有可供大家參考的其他原因。

預設的拒絕存取訊息是什麼?

如果是已登入 WordPress 的使用者,便會顯示正常的 REST API 資料;如果使用者登入,以下便是預設訊息:

{"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。

如果再次登入 WordPress 並對 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.

這個外掛是否能與區塊編輯器搭配使用?

可以,無論使用哪一個編輯器 (傳統編輯器或區塊編輯器),這個外掛的執行方式都一樣。

自訂錯誤訊息的方式為何?

依照預設,這個外掛會對未通過身分驗證的使用者顯示「僅有已驗證的使用者可以使用 REST API」。如需依據需求自訂這個訊息,請在目前使用的佈景主題的 functions.php 中或使用自訂外掛新增以下程式碼:

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');

如何允許存取 Contact Form 7?

依據這篇討論串的說明,Contact Form 7 需要存取 REST API 才能讓聯絡表單正常執行。如果需要允許 Contact Form 7 存取 REST API,請參考這篇文章

如何提問?

請透過這份聯絡表單提出問題或意見反應。

使用者評論

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 則使用者評論

參與者及開發者

以下人員參與了開源軟體〈Disable WP REST API〉的開發相關工作。

參與者

〈Disable WP REST API〉外掛目前已有 8 個本地化語言版本。 感謝全部譯者為這個外掛做出的貢獻。

將〈Disable WP REST API〉外掛本地化為台灣繁體中文版

對開發相關資訊感興趣?

任何人均可瀏覽程式碼、查看 SVN 存放庫,或透過 RSS 訂閱開發記錄

變更記錄

如果 Disable 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