Yes, using the rest_authentication_errors filter:
add_filter( 'rest_authentication_errors', 'filter_incoming_connections' );
function filter_incoming_connections( $errors ){
$allowedAddress = array( '127.0.0.1' );
$requestServer = $_SERVER['REMOTE_ADDR'];
if( ! in_array( $requestServer, $allowedAddress ) )
return new WP_Error( 'forbidden_access', 'Access denied', array( 'status' => 403 ) );
return $errors;
}
-
This reply was modified 7 years, 11 months ago by
bcworkz. Reason: code fixed
@a2hostingr – great answer!
An aside, next time you post code in these forums, please use the code button (or type a backtick) instead of block quote. Code within block quotes cannot be copy/pasted without imparting syntax errors. Not so with code formatted as such. Fixed it for ya this time 🙂
creativ3y3 – if you copied the above code and got syntax errors, try again with the new format.
More and more plugin authors are being lazy and using the REST API to handle requests that are much more efficiently handled with built-in code. Those requests will either come from your server’s IP address, or from a user’s IP address if it was sent via AJAX.
If you are using such a plugin, the code you want to add will probably break the plugin.
Great info guys thanks I will give it a shot