Security PHP Beginner

Block Common Usernames Like “admin”

Stop anyone registering or being created with usernames attackers try first, such as admin, administrator and root.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 11 lines
add_filter( 'illegal_user_logins', function ( array $logins ): array {
	return array_merge( $logins, [
		'admin',
		'administrator',
		'root',
		'test',
		'user',
		'webmaster',
		'support',
	] );
} );

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.

Password-guessing bots start with usernames like “admin”. If no account has one of those names, every one of those attempts fails before the password even matters. This snippet stops those usernames being used for new accounts.

How it works

WordPress checks the illegal_user_logins list whenever a user is created or registers, including in wp-admin, on wp-login.php and through wp_insert_user(), which WooCommerce uses for customer accounts. Matching names are rejected with “Sorry, that username is not allowed.” The check isn’t case-sensitive.

Things to watch

  • Existing accounts aren’t changed. If you already have an “admin” user, create a new administrator, log in as them and delete the old account, assigning its posts to the new one.
  • WooCommerce can generate usernames from email addresses. Those are checked too.

How to test it

Go to Users → Add New and try to create a user called “admin”. You should see “Sorry, that username is not allowed.”

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