Skip to content

Instantly share code, notes, and snippets.

@jshwlkr
Created July 24, 2025 17:55
Show Gist options
  • Select an option

  • Save jshwlkr/84de352f581c18b10289a847e740d5c3 to your computer and use it in GitHub Desktop.

Select an option

Save jshwlkr/84de352f581c18b10289a847e740d5c3 to your computer and use it in GitHub Desktop.
Prevent user enumeration in WordPress for unathenticated users
public function disable_user_endpoint() {
add_filter(
'rest_authentication_errors',
function ( $access ) {
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
return $access;
}
if ( ! is_user_logged_in() || ! current_user_can( 'list_users' ) ) {
$requested_route = filter_var( $_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL );
if ($requested_route !== false && strpos( $requested_route, '/wp/v2/users' ) !== false ) {
return new WP_Error( 'rest_forbidden', 'Sorry, you are not allowed to do that.', array( 'status' => 403 ) );
}
}
return $access;
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment