Users & Login PHP Intermediate

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.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 40 lines
// 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 ] );
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

Don't use with Another hide-login plugin. Check the notes below before adding it.

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.

How it works

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.

Things to watch

  • Change 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.
  • Logged-out visits to /wp-admin/ redirect to the login page, so they’ll see the 404 too. That’s intended.
  • WooCommerce’s My Account login form is a separate page and keeps working for customers.
  • This hides the door; it doesn’t lock it. Keep strong passwords and two-factor login.

How to test it

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.

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