Skip to content

Instantly share code, notes, and snippets.

@chuckreynolds
Created February 20, 2017 02:03
Show Gist options
  • Save chuckreynolds/68fbfdafec2bbca3e6764db877944e16 to your computer and use it in GitHub Desktop.
Save chuckreynolds/68fbfdafec2bbca3e6764db877944e16 to your computer and use it in GitHub Desktop.
WordPress: Disable WP REST API JSON endpoints if user not logged in
<?php
/*
* Disable WP REST API JSON endpoints if user not logged in
*/
function chuck_disable_rest_endpoints( $access ) {
if( ! is_user_logged_in() ) {
return new WP_Error( 'rest_cannot_access', __( 'Only authenticated users can access the REST API.', 'disable-json-api' ), array( 'status' => rest_authorization_required_code() ) );
}
return $access;
}
add_filter( 'rest_authentication_errors', 'chuck_disable_rest_endpoints' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment