Last active
February 27, 2019 14:01
-
-
Save avillegasn/728f4ed1160026db14c6160dea3b05ef to your computer and use it in GitHub Desktop.
Code snippets to hide/manage routes in WordPress REST API
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_authentication_errors', function( $result ) { | |
if ( ! empty( $result ) ) { | |
return $result; | |
} | |
if ( ! is_user_logged_in() ) { | |
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) ); | |
} | |
if ( ! current_user_can( 'administrator' ) ) { | |
return new WP_Error( 'rest_not_admin', 'You are not an administrator.', array( 'status' => 401 ) ); | |
} | |
return $result; | |
}); |
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_authentication_errors', function( $result ) { | |
if ( ! empty( $result ) ) { | |
return $result; | |
} | |
if ( ! is_user_logged_in() ) { | |
return new WP_Error( 'rest_not_logged_in', 'You are not currently logged in.', array( 'status' => 401 ) ); | |
} | |
return $result; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment