Created
October 14, 2015 18:37
-
-
Save joehoyle/b944d1984701df9dc494 to your computer and use it in GitHub Desktop.
Only allow access to the API for authenticated requests
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
<?php | |
add_filter( 'rest_pre_dispatch', function() { | |
if ( ! is_user_logged_in() ) { | |
return new WP_Error( 'not-logged-in', 'API Requests are only supported for authenticated requests', array( 'status' => 401 ) ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd stress that if you want to lock down the API, you'll probably want to use a capability check rather than
is_user_logged_in
(such ascurrent_user_can( 'edit_posts' )
) as being logged in doesn't meant much if they are logged in as a user with no capabilities.