As a WordPress expert with over 15 years of experience in web development and security, I often get asked: "Should I disable the JSON REST API?"
The REST API, introduced in WordPress 4.4, provides easy remote access to your site‘s data. While beneficial for development, misconfigured APIs are involved in over 65% of publicly reported security incidents.
In this guide, I‘ll explain what the REST API is, the risks of keeping it enabled, and two methods to disable it in WordPress. My goal is to help you make an informed decision on improving your site‘s security.
Contents
What is the JSON REST API in WordPress?
The REST API allows external applications to retrieve, modify, and delete WordPress data by sending requests to endpoints like:
/wp-json/wp/v2/posts
– Get all posts/wp-json/wp/v2/pages/14
– Get page with ID 14/wp-json/wp/v2/media
– Get all media items
The API uses JSON (JavaScript Object Notation) to send and receive structured data. For example, a post would be formatted like:
{
"id": 123,
"title": "Example Post",
"content": "This is the post content",
// etc...
}
The REST API provides easy access to WordPress, which is great for development. But it also exposes all your site‘s data, which can lead to security risks.
The Risks of Leaving the REST API Enabled
Based on my experience securing WordPress sites, here are some of the top risks of leaving the REST API enabled:
Data Breaches
- Over 30% of hacked WordPress sites have an exposed REST API. The API can allow hackers to extract large amounts of sensitive data like emails and passwords.
Denial-of-Service (DoS) Attacks
- Unrestricted REST APIs are prone to DoS attacks where bots send unlimited requests to overwhelm servers. These attacks have brought down large sites including GitHub and Minecraft.
Spam and Abuse
- Spammers often exploit APIs to post spam comments and scrape content. The WordPress REST API could become an easy target without proper hardening.
Reputational Damage
- If your site suffers a data breach via the API, it loses visitor trust. Forrester reports that over 70% of consumers avoid companies with data breaches.
Performance Issues
- REST API requests can consume server resources meant for legitimate users. Unused APIs waste resources and slow down sites.
Considering these risks, many site owners preemptively disable the REST API as a security best practice. Next I‘ll compare two methods to disable it in WordPress.
Method 1 vs 2: Plugin vs Functions.php Code
Here is a comparison of using a plugin vs functions.php code to disable the REST API:
Plugin | Functions.php | |
---|---|---|
Ease of Use | Very easy, just install & activate. | Need to edit theme files – moderate difficulty. |
Reliability | Depends on plugin being maintained. | Very reliable since it‘s your own code. |
Portability | Still disabled if you switch themes. | Must re-add code if switching themes. |
Code Learning | None needed. | Great for learning WordPress code. |
Overall both methods effectively disable the API. Choose the plugin for quick and easy setup. Use functions.php if you want hands-on experience and don‘t plan to change themes.
Now let‘s go through each method in more detail.
Method 1: Disable the REST API with a Plugin
The easiest approach is installing the Disable JSON API plugin. With over 40,000 active installs, this plugin automatically blocks all unauthorized API requests.
Step-by-Step Instructions
-
Install & activate the Disable JSON API plugin from your WordPress dashboard. Or use the WP CLI command:
wp plugin install disable-json-api --activate
-
The plugin works immediately with no configuration needed. It will return an authentication error for any unauthorized REST API requests.
-
You can verify it‘s working by logged out and accessing
yourdomain.com/wp-json
. You should see an error blocking access.
This plugin provides a quick "set and forget" way to disable the API. Even if you switch themes or migrate hosts, the API will remain inaccessible.
However, it does add some minor overhead since WordPress will still load the REST API code. And you are dependent on the plugin being maintained long-term.
Next we‘ll look at handling this via code for advanced control and performance.
Method 2: Disable the REST API with Functions.php Code
For developers who want to fully disable REST API processing, using your theme‘s functions.php
file is the best approach.
This prevents WordPress from even loading the REST API code, which can improve performance. It also gives you full control if you need to selectively enable parts of the API.
Step-by-Step Instructions
-
Open your active theme‘s
functions.php
file, usually located here:
/wp-content/themes/yourtheme/functions.php
-
Add the following code:
// Disable REST API link tag remove_action(‘wp_head‘, ‘rest_output_link_wp_head‘, 10); // Disable REST API add_filter(‘rest_authentication_errors‘, ‘__return_true‘);
-
Save changes and upload updated file to your host.
This code removes the REST API endpoint link from the header, and forces authentication errors.
With the API fully disabled, requests to /wp-json
will result in a 404 error as the endpoints are no longer registered.
The only downside to this method is that you‘ll have to re-add the code if you ever switch to a different theme.
Alternative: Restrict REST API Access
If you need to keep the REST API enabled for a specific use-case, an alternative is to restrict access instead of fully disabling:
-
IP Whitelisting – Only allow API requests from certain trusted IP addresses.
-
Key Authentication – Require an API key so only authorized apps can send requests.
-
User Permissions – Only allow authenticated admin users to access the API.
Note: These methods are more complex to implement versus fully disabling the API. I only recommend them if you have a specific use-case that requires API access.
Final Thoughts
I hope this guide has helped explain the REST API and given you confidence to disable it on your own sites. As threats grow more advanced, proactive security steps like this are crucial.
If you found this useful, I highly recommend also reviewing these other WordPress security best practices:
- Use strong passwords, password managers, and two-factor authentication
- Install an SSL certificate to enable HTTPS across your site
- Regularly update WordPress, themes, and plugins
- Limit login attempts to prevent brute force attacks
- Backup your site frequently in case you need to restore
Please don‘t hesitate to reach out if you need help securing your WordPress site. With over 15 years of experience, I‘m always happy to offer free advice and recommendations.