Skip to content

Instantly share code, notes, and snippets.

@avillegasn
Last active February 27, 2019 14: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 avillegasn/728f4ed1160026db14c6160dea3b05ef to your computer and use it in GitHub Desktop.
Save avillegasn/728f4ed1160026db14c6160dea3b05ef to your computer and use it in GitHub Desktop.
Code snippets to hide/manage routes in WordPress REST API
<?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;
});
<?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