Disable WP Rest API V2

By | May 31, 2017

Disable WordPress WP REST API V2.

WordPress WP REST API V2 give many advantages for website developers to make an interaction in a wordpress website. It can be used to create, fetch, edit and also delete posts , taxonomies and also retrieve another information from a website in JSON format.

But not all of website’s owners need or love this function. By some reasons some website’s owner or web developers want to disable this function. Some of that reason are :

  1. Security
    In version 4.7 and 4.7.1 of wordpress there is Content Injection Vulnerability you can read more details in sucuri blog.
  2. Protect Content.
    With WP-JSON Api someone can viewing posts data in JSON format. It will makes a website’s scrapper more easily to scrape a wordpress websites’s content. It will not 100% protected your website’s content but it will be a bit harder to a scrapper to grab your content.

Disable Rest API V2 With Plugin

Now if you want to disable wp json api for your blog you can do it by installing Disable REST API Plugin.

** As of WordPress 4.7, the filter provided for disabling the REST API has been removed. However, this plugin will now
forcibly return an authentication error to any API requests from sources who are not logged into your website, which
will effectively still prevent unauthorized requests from using the REST API to get information from your website **

You can download it here : https://wordpress.org/plugins/disable-json-api/ or directly from your wordpress dashboard.

Disable Rest API V2 Without Plugin

If you do not want to use a plugin , in your theme function. (function.php) add this following code.

add_filter( 'rest_authentication_errors', function( $result ) {
if ( ! empty( $result ) ) {
    return $result;
}
if ( ! is_user_logged_in() ) {
    return new WP_Error( 'restx_logged_out', 'Sorry, you must be logged in to make a request.', array( 'status' => 401 ) );
}
return $result;
});

Related Posts

wp rest api v2

WP Rest API V2 Introduction

WP Rest API V2 Introduction. WordPress is one of the most used content management system (CMS) in the world.

wp rest api v2

WP REST API Get Featured Image With Custom Endpoint

WP REST API Get Featured Image By Adding Custom Endpoint in Theme Function

wp rest api v2

WP REST API Create Category PHP cURL

Wordpress Tutorial WP REST API V2 Create Category With PHP cURL with code example

WP REST API V2 Auth

WP REST API Basic Authentication PHP File Get Contents

Wordpres Tutorial WP REST API Basic Authentication PHP File Get Contents. At previous post I already posted about wp rest api basic authentication with php cURL, but in some cases a hosting / server doesn’t has cURL module ( not installer , not enabled ).

WP REST API V2 Auth

WP REST API Create User With PHP cURL

WP REST API Create User With PHP cURL. PHP code example to create user for your wordpress website.