Skip to content

Instantly share code, notes, and snippets.

@w33zy
Created February 14, 2017 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w33zy/ffb2733ef9f34e8eab4f03d463d0208f to your computer and use it in GitHub Desktop.
Save w33zy/ffb2733ef9f34e8eab4f03d463d0208f to your computer and use it in GitHub Desktop.
WordPress REST API access only to logged in users.
/* ------------------------------------------------------------------------- *
* Returning an authentication error if a user who is not logged in tries to query the REST API
/* ------------------------------------------------------------------------- */
function only_allow_logged_in_rest_access( $access ) {
if( ! is_user_logged_in() ) {
return new WP_Error( 'rest_API_cannot_access', 'Only authenticated users can access the REST API.', array( 'status' => rest_authorization_required_code() ) );
}
return $access;
}
add_filter( 'rest_authentication_errors', 'only_allow_logged_in_rest_access' );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'template_redirect', 'rest_output_link_header', 11 );
remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment