Created
February 14, 2017 16:01
-
-
Save w33zy/ffb2733ef9f34e8eab4f03d463d0208f to your computer and use it in GitHub Desktop.
WordPress REST API access only to logged in users.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ------------------------------------------------------------------------- * | |
* 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