Hide the WordPress login page behind a secret key, without a plugin
Every bot on the internet knows /wp-login.php, and a new site gets password guessing attempts within hours. Moving or hiding the login page does not make passwords stronger, but it removes almost all of this automated noise: fewer requests, smaller logs, less load. Plugins like WPS Hide Login do it with a custom slug. The approach below is smaller and has fewer ways to break: wp-login.php stays where it is, but answers with a 404 unless the visitor knows a secret key.
Problem: The login page receives constant brute force attempts, and you do not want another plugin for hiding it.
Solution: On the login_init hook, let the request through only when it carries the secret key in the URL or a cookie that was set after a request with the key; answer everything else with 404. Keep the actions that must work without the key: logout, password reset links from emails and password-protected posts.
The code
In a small mu-plugin, for example wp-content/mu-plugins/ha-login-key.php:
<?php
/**
* Plugin Name: Login key
*/
define( 'HA_LOGIN_KEY', 'change-me-to-a-long-random-string' );
add_action( 'login_init', function () {
$action = isset( $_REQUEST['action'] ) ? sanitize_key( $_REQUEST['action'] ) : 'login';
// These must work for everybody: logout (has its own nonce), links from emails, protected posts
if ( in_array( $action, array( 'logout', 'rp', 'resetpass', 'postpass', 'confirmaction' ), true ) ) {
return;
}
$cookie = 'ha_login_key';
if ( isset( $_GET['ha_key'] ) && hash_equals( HA_LOGIN_KEY, (string) wp_unslash( $_GET['ha_key'] ) ) ) {
setcookie( $cookie, wp_hash( HA_LOGIN_KEY ), time() + HOUR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
return;
}
if ( isset( $_COOKIE[ $cookie ] ) && hash_equals( wp_hash( HA_LOGIN_KEY ), (string) $_COOKIE[ $cookie ] ) ) {
return;
}
status_header( 404 );
nocache_headers();
exit( 'Not found' );
} );
The login address is now https://example.com/wp-login.php?ha_key=change-me-to-a-long-random-string. After opening it once, the cookie keeps the login form working for an hour, including the POST of the form and a wrong password, which reload the page without the key. The cookie holds a hash of the key, not the key itself.
What still works
- /wp-admin/ for logged-in users, as before.
- For guests, /wp-admin/ redirects to wp-login.php, which now answers 404: the admin URL no longer reveals the login page.
- Logout links, password reset emails and the password form of protected posts.
- WooCommerce’s “My account” login and other front-end login forms, because they do not use wp-login.php.
What it does not do
It does not protect xmlrpc.php, which also accepts passwords: block it if nothing on the site uses it, as described in protecting wp-config.php and xmlrpc.php. It does not replace strong passwords or two-factor authentication for administrators. And a key in a URL can end up in browser history or in a screenshot, so treat it like a weak password, not like a secret.
Keep the key somewhere safe. If you lose it, remove or rename the mu-plugin file by FTP and log in as usual; recovering a login hidden by a plugin works the same way, see getting back to a login page hidden with WPS Hide Login.
NOTE: Test logout, a password reset and a wrong password before relying on it, and tell every user of the site the new address. Some security plugins and hosts add their own login protection; combined with this, make sure the two do not block each other.