Prevent username enumeration in WordPress
Half of a login is the username, and WordPress gives it away in several places: /?author=1 redirects to /author/admin/, the REST API lists users at /wp-json/wp/v2/users, author archives and oEmbed data show author names. Bots collect these usernames and then only have to guess passwords. Closing these doors is cheap; it does not replace strong passwords, but it takes away the easy half.
Problem: Visiting /?author=1 or /wp-json/wp/v2/users shows the login names of the site’s users to anybody.
Solution: Block ?author=N requests for guests, limit the users REST endpoint to logged-in users, and make sure the public slug of each user (the “nicename”) differs from the login name.
?author=N
<?php
add_action( 'template_redirect', function () {
if ( ! is_user_logged_in() && isset( $_GET['author'] ) ) {
status_header( 404 );
nocache_headers();
include get_404_template();
exit;
}
} );
Or in .htaccess, before the WordPress block:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/wp-admin [NC]
RewriteCond %{QUERY_STRING} (^|&)author=\d+ [NC]
RewriteRule ^ - [R=404,L]
</IfModule>
The REST API
<?php
add_filter( 'rest_endpoints', function ( $endpoints ) {
if ( ! is_user_logged_in() ) {
unset( $endpoints['/wp/v2/users'], $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
}
return $endpoints;
} );
Remove the endpoints only for guests. Logged-in editors and authors need them: the block editor loads the author dropdown from /wp/v2/users, and removing it for users without list_users breaks the editor for everybody except administrators.
Author slugs and archives
The real problem is that the author slug in /author/admin/ is usually the login name. WordPress creates user_nicename from the login and never changes it. Set a different one for each user who can log in to the admin:
wp user update admin --user_nicename=anna-editor
Now author archives show /author/anna-editor/, and the login name appears nowhere. If the site does not need author archives at all (a company site with one author), turn them into a 404 for everybody:
<?php
add_action( 'template_redirect', function () {
if ( is_author() ) {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
}
} );
A 404 is the honest answer for a page that does not exist; redirecting author pages to the home page looks like a “soft 404” to search engines.
Login errors
WordPress says “The username admin is not registered on this site” for a wrong username, and “The password you entered for the username anna is incorrect” for a wrong password, which also confirms that a username exists. A generic message closes that too:
<?php
add_filter( 'login_errors', function () {
return __( 'Wrong username or password.', 'textdomain' );
} );
NOTE: Usernames are not secrets: they can still leak through old posts, emails or other sites. Treat these measures as noise reduction and back them with strong passwords, two-factor authentication for administrators and a limit on failed login attempts.