Last Updated on December 11, 2023 by Mike Kipruto
Today I want to share a quick and easy tutorial on how to disable the WordPress Rest API for nonlogged and redirect them to home in WordPress using custom code.
To achieve this, we will use the following code that hooks into the WordPress “rest_authentication_errors” filter hook.
/**
* How to Disable the WordPress REST API Requests The Easy Way
* @link https://kipmyk.co.ke/code/how-to-disable-the-wordpress-rest-api-requests-the-easy-way/
*/
add_filter( 'rest_authentication_errors', function ( $result ) {
// If the user is not logged in, redirect to the home page.
if ( ! is_user_logged_in() ) {
wp_redirect( home_url() );
exit;
}
return $result;
});