REST API blocked for anyone not logged

[ad_1]

Plugin Support
Amir

(@amirfallah)

Hi,

Thank you for bringing this issue to our attention. The document indeed needed updating.

For this scenario, you can use the following code:

add_filter('rest_authentication_errors', 'allow_rest_api_for_local_ips');

function allow_rest_api_for_local_ips($errors)
{
// if there is already an error, just return it
if (is_wp_error($errors)) {
return $errors;
}

$whitelist = array(
'127.0.0.1',
// etc
);

if (!in_array($_SERVER['REMOTE_ADDR'], $whitelist)) {
// return WP_Error object if user is not logged in
return new WP_Error('no_rest_api_sorry', 'REST API not allowed', array('status' => 401));
}

return $errors;
}

Please let me know if you have any further questions or need assistance.
Regards,

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer