? The Snippet:
add_filter('json_enabled', '__return_false'); add_filter('json_jsonp_enabled', '__return_false');
ℹ️ About the Snippet
As of WordPress 4.7 release, the WordPress REST API was enabled by default which was rolled out in WordPress 4.4. WordPress REST API is a great feature only if we need it. The biggest issue after enabling by default is — it can provide access to some data like posts, pages, users information without requiring any authentication. In this guide, we will learn how to disable JSON REST API in WordPress.
After using this code snippet, no one will be able to access the content of your website through the REST API endpoints as it’ll disable the JSON response.
✅ How to use the Snippet
There are plugins to disable the REST API in WordPress, but with this small code snippet, you can do it without adding one extra plugin. You only need to have a basic understanding of file editing in WordPress. Below are the steps you’ve to follow.
- Edit functions.php
Go to your theme directory either by FTP or from Appearance->Editor. If you’re on localhost, you can open the theme files in any Code Editor/IDE.
- Paste the below Code Snippet
Scroll down to end of the page and add the below code snippet to functions.php with a helping comment in case if you forget what this code is about in future.
// Disable JSON WordPress REST API add_filter('json_enabled', '__return_false'); add_filter('json_jsonp_enabled', '__return_false');