Make wp-login.php return a 404 unless the visitor arrives through your secret link, without breaking logouts, password resets or saved logins.
// Change this to a long, random value and keep it private.
const NAHNU_LOGIN_SECRET = 'change-this-to-something-long-and-random';
/**
* Bookmark: https://example.com/wp-login.php?nahnu=change-this-to-something-long-and-random
* Visiting that link once sets a cookie, so the login page keeps working on that browser.
*/
add_action( 'login_init', function (): void {
$action = sanitize_key( $_REQUEST['action'] ?? 'login' );
$cookie = 'nahnu_login_' . COOKIEHASH;
$token = wp_hash( NAHNU_LOGIN_SECRET );
$allowed = [ 'logout', 'rp', 'resetpass', 'postpass', 'confirmaction' ];
// Always let these through: logged-in users, form submissions and emailed links.
if ( is_user_logged_in() || 'POST' === $_SERVER['REQUEST_METHOD'] || in_array( $action, $allowed, true ) ) {
return;
}
// The secret link: remember this browser for 30 days.
if ( isset( $_GET['nahnu'] ) && hash_equals( NAHNU_LOGIN_SECRET, (string) wp_unslash( $_GET['nahnu'] ) ) ) {
setcookie( $cookie, $token, [
'expires' => time() + 30 * DAY_IN_SECONDS,
'path' => COOKIEPATH ?: '/',
'secure' => is_ssl(),
'httponly' => true,
'samesite' => 'Lax',
] );
return;
}
// A browser that used the secret link before.
if ( isset( $_COOKIE[ $cookie ] ) && hash_equals( $token, (string) $_COOKIE[ $cookie ] ) ) {
return;
}
// Everyone else sees a normal 404 page.
status_header( 404 );
nocache_headers();
wp_die( 'Page not found.', 'Page not found', [ 'response' => 404 ] );
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Bots hammer wp-login.php all day because every WordPress site has one at the same address. This snippet makes that page return a 404 to everyone except people who arrive through your secret link. The old version of this snippet only redirected the login page to the homepage, which hid nothing and could also block registrations.
The check runs on login_init, which fires at the top of wp-login.php. A few things always get through so nothing breaks: logged-in users, form submissions (the login form itself posts back to wp-login.php), logouts, password reset links from emails, password-protected posts and privacy request confirmations.
Everything else needs the secret. Visiting wp-login.php?nahnu=your-secret sets an HttpOnly cookie for 30 days, so after that one visit the normal login page works in that browser, including the “session expired” pop-up in wp-admin. The cookie holds a hash of the secret rather than the secret itself, and both checks use hash_equals() to avoid timing leaks.
NAHNU_LOGIN_SECRET before you use it, and bookmark your secret link. If you lose it, remove the snippet over FTP or your host’s file manager./wp-admin/ redirect to the login page, so they’ll see the 404 too. That’s intended.In a private window, open /wp-login.php. You should get a 404. Open your secret link, and the login form appears. Close the window, open a new private one and visit the plain address again: it’s a 404, because private windows don’t keep the cookie.