Skip to content

Instantly share code, notes, and snippets.

@techjewel
Created February 8, 2021 20:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save techjewel/27a6c03e2f0509ac9b6372fd7ef3769d to your computer and use it in GitHub Desktop.
Save techjewel/27a6c03e2f0509ac9b6372fd7ef3769d to your computer and use it in GitHub Desktop.
<?php
/*
* Disable users REST endpoint for public or not author users users
* To access the endpoint the user must need to be contributor
* aka need to have edit_posts permission
*/
add_filter( 'rest_endpoints', function ($endpoints) {
$endpoints_to_remove = array(
'/wp/v2/users'
);
if ( !current_user_can('edit_posts') ) {
foreach ( $endpoints_to_remove as $rem_endpoint ) {
foreach ( $endpoints as $maybe_endpoint => $object ) {
if ( stripos( $maybe_endpoint, $rem_endpoint ) !== false ) {
unset( $endpoints[ $maybe_endpoint ] );
}
}
}
}
return $endpoints;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment