Skip to content

Instantly share code, notes, and snippets.

@johnalarcon
Created May 12, 2021 18:32
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 johnalarcon/9ad93f247bfdcee8edb21cd9c26f7e1e to your computer and use it in GitHub Desktop.
Save johnalarcon/9ad93f247bfdcee8edb21cd9c26f7e1e to your computer and use it in GitHub Desktop.
Remove the /users/ REST API endpoint in WordPress and ClassicPress
/**
* The following code will remove access to the /users/ endpoint. This makes the
* endpoint inaccessible while not blocking access to other endpoints. This code
* works with both WordPress and ClassicPress.
*
* Why do this? The REST API is very handy, however, by default, it exposes data
* that you may not want exposed – namely, your site's usernames. With a list of
* your usernames, the success of a brut-force attack becomes more likely.
*
*/
function codepotent_disable_rest_user_endpoint($endpoints) {
$route = '/wp/v2/users';
if (isset($endpoints[$route])) {
unset($endpoints[$route]);
}
if (isset($endpoints[$route.'/(?P<id>[\d]+)'])) {
unset($endpoints[$route.'/(?P<id>[\d]+)']);
}
return $endpoints;
}
add_filter('rest_endpoints', 'codepotent_disable_rest_user_endpoint');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment