Disabling JSON REST API in WordPress: Quick Guide

Disabling the JSON REST API in WordPress may be a necessity for some administrators looking to increase the security of their sites. While this WordPress functionality offers significant advantages, such as interaction with external applications, it can sometimes pose a risk if used incorrectly. Below, we will discuss how to effectively disable this API.

What is the JSON REST API in WordPress?

The JSON REST API is a feature of WordPress that allows applications and services to interact with site content programmatically. This means that you can access your WordPress posts, pages and other data through HTTP requests.

This API makes it easy to create applications that can be integrated with WordPress, such as mobile apps or custom dashboards. However, can also be an attack vector if not properly managed..

Why disable the JSON REST API?

There are several reasons why you might consider disabling the JSON REST API in your WordPress installation:

  • Safety: By disabling the API, you reduce the risk that malicious attackers can exploit vulnerabilities.
  • Privacy: You avoid exposing sensitive data that could be accessible through the API.
  • Performance: Although the overhead is minimal, disabling the API may improve performance in some cases.

Risks associated with the JSON REST API

The JSON REST API allows unauthenticated users to access certain WordPress data. This can include information about posts, comments, and other details that, while not critical, can be used for brute force attacks or information gathering.

Disabling JSON REST API in WordPress: Quick Guide
Download our web maintenance guide Free of charge!
Free guide for freelancers and small businesses that want to avoid surprises and improve their web performance.

Therefore, if your site does not use external applications that depend on this API, it is advisable to consider disabling it.

How to disable the JSON REST API in WordPress

There are several ways to disable the JSON REST API. Here are two common methods you can use.

Method 1: Modification of the functions.php file

A simple way to disable the JSON REST API is through the file functions.php of your theme. Adding the following code will prevent API access for all unauthenticated users:

function disable_json_rest_api() {
    if (!is_user_logged_in()) {
        wp_die('REST API is disabled for unauthenticated users.');
    }
}
add_action('rest_authentication_errors', 'disable_json_rest_api');

Method 2: Use a plugin

If you prefer not to touch the code, you can use a security plugin that includes options to disable the JSON REST API. Some popular plugins are:

  • Disable REST API: This plugin disables API access for all unauthenticated users.
  • Wordfence Security: It provides a variety of security features, including the option to disable the API.

Verification of deactivation

After implementing one of the above methods, it is important to verify that the JSON REST API is disabled. You can do this by entering the following URL in your browser:

https://tusitio.com/wp-json/wp/v2/posts

If you have disabled the API correctly, you should receive an error message or a warning that access is restricted.

Final considerations

Disabling the JSON REST API may be a wise decision if you do not use external applications that depend on it. However, if at some point you decide to integrate functionality that requires API access, remember that you can re-enable it by following similar steps. The key is to find a balance between security y functionality.