How to Disable WP REST API

WordPress Rest API helps to GET the JSON objects from the WP endpoint. By using WP Rest API, you can GET all the data from the website and use it on any platform which you like. WP Rest API allows developers to use in the Mobile Application, Angular, or any other applications. ( E.g. Here we used WordPress posts in Angular App to display all the posts by using WP Rest API. )
The WordPress Rest API endpoints work with the below example URL.
1 |
https://softaox.info/wp-json/ |
Disabling the WordPress Rest API
The benefits of disabling the WordPress Rest API gives protection from hackers and unauthorized users. The WP Rest API is default given by the WordPress, we need to disable or access to allow with the username and password.
The below code will help you disable Rest API from unauthorized user access.
1 2 3 4 5 6 7 8 9 10 11 |
// Disable WP Rest API add_filter( 'rest_authentication_errors', function( $result ) { if ( ! empty( $result ) ) { return $result; } // If User Not Logged In if ( ! is_user_logged_in() ) { return new WP_Error( 'rest_not_logged_in', 'You do not have access to Use.', array( 'status' => 401 ) ); } return $result; }); |
Below you can see after disabling the WP Rest API output.
An alternative to Disable the WP Rest API with WordPress Plugin
Using the plugin is the simplest way to disable the WP Rest API but this will make some more weight to your website also plugins are third-party.
Use the Disable REST API to disable.
In Admin Dashboard Settings -> Disable Rest API
In the above screen, you can see they are many ways to disabling the JSON data. Benefits of using plugin easy handling with Frontend UI and get various options.
Conclusion:
I hope this will help you to disable the WP Rest API either with custom code without plugin or by using a plugin. If you have any questions about this topic let me know in the comment section. If you like this article share it with your friends.
Rest APIWithout PluginwordpressWP Rest API
Mraj
Creative Designer & Developer specialist by the spirit and a loving blogger by thoughts. If you have any questions let me drop an email with the article name to the following email id: [email protected]