Security Guide Intermediate 5 min

Hardening WordPress, Part 3: Protect the Login From Bots and Stolen Passwords

Bots try passwords on every WordPress login page, all day. Stop handing them usernames, slow down guessing, close XML-RPC and other side doors, and add a second factor so a stolen password isn't enough.

Hardening WordPress · Part 3 of 4
TutorialIntermediate20 minTested on WordPress 7.1Updated November 17, 2026

In part 1 we locked down wp-config.php, and in part 2 we added headers that protect visitors’ browsers. This part is about the front door: the login. Every WordPress site has one at the same address, so bots try passwords on yours whether your site is big or small. The aim is simple: make guessing slow and pointless, and make sure you’d notice if someone did get in.

Before you start

  • An administrator accountAnd a second way in, such as FTP or your host’s file manager, in case you lock yourself out
  • A way to add PHP snippetsChild theme, must-use plugin or a snippets plugin
  • A private browser windowTo test each change as a logged-out visitor

How bots attack a WordPress login

Three things make the login an easy target:

  • Usernames are easy to find. WordPress lists authors at /wp-json/wp/v2/users, and /?author=1 redirects to your first user’s archive with the username in the address. Its login errors also say whether a username exists.
  • Guessing is unlimited. WordPress lets anyone try passwords as fast as they like, on wp-login.php and through XML-RPC.
  • Passwords get reused. If one of your users’ passwords leaked from another site, bots will try it on yours.

Each section below closes one of these gaps.

Stop giving away usernames

A bot that knows your username only has to guess the password. This snippet removes the public users endpoint for logged-out visitors and stops /?author=1 revealing a username. Logged-in requests, including the block editor, keep working.

functions.phpPHP
// Block unauthenticated access to the REST API users endpoint
add_filter( 'rest_endpoints', function( $endpoints ) {
    if ( ! is_user_logged_in() ) {
        unset( $endpoints['/wp/v2/users'] );
        unset( $endpoints['/wp/v2/users/(?P<id>[\d]+)'] );
    }
    return $endpoints;
} );

// Prevent user enumeration via ?author=N URL redirect
add_action( 'init', function() {
    if (
        ! is_admin() &&
        isset( $_REQUEST['author'] ) &&
        ! current_user_can( 'list_users' )
    ) {
        wp_redirect( home_url( '/' ), 301 );
        exit;
    }
} );
Tested on WordPress 7.1 · PHP 8.3Checked November 17, 2026

Then make the login form give one message for every failed attempt, so it no longer confirms which usernames are real:

Snippet

Show One Generic Login Error

Replace "The password you entered for the username admin is incorrect" with one message that doesn't confirm whether a username exists.

PHPBeginner
View
Change display names too

WordPress shows a user’s display name in bylines and comments, and by default it’s the login username. Under Users → Profile, set Display name publicly as to something other than the username for every author.

You should now see

In a private window, yoursite.com/wp-json/wp/v2/users returns a 404, yoursite.com/?author=1 goes to the homepage, and a wrong username and a wrong password show the same login error.

Slow down password guessing

This snippet counts failed logins for each IP address and locks that address out for 15 minutes after five failures. It works on wp-login.php, WooCommerce’s My Account login and anything else that signs in through wp_signon(). During a lockout even the right password is refused, so a bot can’t keep guessing in the background.

functions.phpPHP
const NAHNU_MAX_LOGIN_ATTEMPTS = 5;
const NAHNU_LOCKOUT_MINUTES     = 15;

function nahnu_login_attempts_key(): string {
	return 'nahnu_ll_' . md5( (string) ( $_SERVER['REMOTE_ADDR'] ?? '' ) );
}

// Refuse logins from locked-out addresses, even with the right password.
add_filter( 'authenticate', function ( $user ) {
	if ( (int) get_transient( nahnu_login_attempts_key() ) >= NAHNU_MAX_LOGIN_ATTEMPTS ) {
		return new WP_Error( 'nahnu_locked_out', sprintf( '<strong>Error:</strong> Too many failed logins. Please try again in %d minutes.', NAHNU_LOCKOUT_MINUTES ) );
	}
	return $user;
}, 30 );

// Count failures per IP address.
add_action( 'wp_login_failed', function (): void {
	$key = nahnu_login_attempts_key();
	set_transient( $key, (int) get_transient( $key ) + 1, NAHNU_LOCKOUT_MINUTES * MINUTE_IN_SECONDS );
} );

// Reset after a successful login.
add_action( 'wp_login', function (): void {
	delete_transient( nahnu_login_attempts_key() );
} );
Tested on WordPress 7.1 · PHP 8.3Checked November 17, 2026
Behind Cloudflare or another proxy?

The snippet reads the visitor’s IP address from REMOTE_ADDR. Behind a proxy that is the proxy’s address, so every visitor would share one counter and five bad logins would lock out everyone. Ask your host to restore real visitor IPs before you add it.

I locked myself out3 common fixes
  • Wait 15 minutes. The lockout clears itself.
  • In a hurry? Delete the rows starting with _transient_nahnu_ll_ from the wp_options table in phpMyAdmin.
  • Locked out repeatedly? Check that a password manager or an old app isn’t retrying an old password in the background.

Close the side doors

The login page isn’t the only way to try a password.

XML-RPC is an old remote-publishing interface that’s still switched on by default. Its system.multicall method lets a bot test hundreds of passwords in a single request, which also slips past per-request limits. Unless you use Jetpack or the WordPress mobile app, turn it off:

Snippet

Block XML-RPC Completely

Turn off XML-RPC, hide the pingback header and refuse direct requests to xmlrpc.php, a favourite target for brute-force attacks.

PHPBeginner
View

Application passwords let apps and services sign in to the REST API for a user. They’re useful for integrations, but if nothing on your site uses them, they’re one more way in for someone who gets into an account:

Snippet

Disable Application Passwords

Turn off application passwords, which let apps access your site through the REST API, if nothing on your site uses them.

PHPBeginner
View
Check before you switch these off

Jetpack and the official WordPress mobile apps connect over XML-RPC. Some backup, deployment and Zapier-style services use application passwords. If you’re not sure, look at the Application Passwords section on each administrator’s profile: anything listed there is in use.

Make stolen passwords useless

Everything above slows attackers down. This section is what actually stops them.

Require long passwords. WordPress warns about weak passwords but lets users tick “Confirm use of weak password” and carry on. This snippet sets a real minimum of 12 characters, and refuses passwords that contain the username:

Snippet

Require Stronger Passwords

Reject passwords shorter than 12 characters or containing the username when users set or reset their password.

PHPIntermediate
View

Turn on two-factor login for everyone who can edit the site. With a second factor, such as a code from an authenticator app or a security key, a leaked or guessed password is not enough to sign in. WordPress doesn’t include two-factor login, so use a plugin. The free Two Factor plugin is maintained by WordPress contributors and supports authenticator apps, email codes and backup codes. For passkeys or hardware security keys, add its companion plugin, Two-Factor Provider: WebAuthn. Start with administrators, then editors and shop managers.

Save the backup codes

When you turn on two-factor login, save the backup codes in your password manager. They are how you get back in if you lose your phone.

Know when someone gets in

If someone does log in with an administrator’s password, the owner of that account should find out straight away. This snippet emails administrators when their account signs in from an IP address it hasn’t used recently:

Snippet

Email Administrators When They Log In From a New IP

Send administrators an email when their account logs in from an IP address it hasn't used before.

PHPIntermediate
View

Expect a few extra emails if you log in from a phone or a connection that changes IP address often. That’s the trade-off for knowing.

What about hiding the login page?

Moving or hiding wp-login.php stops most bots from finding it, which means quieter logs and less load on your server. It’s a reasonable extra, as long as you treat it as one: it hides the door but doesn’t lock it. Anyone who finds the address faces the same login as before. If you want it, our snippet keeps logouts, password resets and saved logins working:

Snippet

Hide the Login Page Behind a Secret Link

Make wp-login.php return a 404 unless the visitor arrives through your secret link, without breaking logouts, password resets or saved logins.

PHPIntermediate
View

Add it last, after everything above, and bookmark your secret link before you log out.

Login hardening checklist

0 / 9

Summary

Start by giving away less: block username lookups and use one generic login error. Then limit failed logins and switch off XML-RPC if you don’t need it. The two changes that matter most are long passwords and two-factor login, because they make a guessed or stolen password useless. Add login alerts so you’d know if something slipped through. Hiding the login page is optional, and only worth doing once the rest is in place.

Next in this series: Part 4, keeping plugins patched and giving every account only the access it needs.

Check your understanding

1Why switch off XML-RPC if you limit failed logins?
2Which change makes a stolen password useless on its own?
3You’re behind Cloudflare and add the login limit snippet without restoring real IPs. What happens?

Was this helpful?

Written by Nahnu Plugins

We build and maintain WordPress plugins. Every guide is tested on a fresh WordPress site before it goes out, and checked again when WordPress updates.

More guides

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only