Using WordPress ‘illegal_user_logins’ PHP filter

The illegal_user_logins WordPress PHP Filter allows you to modify the list of disallowed usernames.

Usage

add_filter('illegal_user_logins', 'custom_illegal_user_logins');

function custom_illegal_user_logins($usernames) {
    // your custom code here
    return $usernames;
}

Parameters

  • $usernames (array): Array of disallowed usernames.

More information

See WordPress Developer Resources: illegal_user_logins

Examples

Block additional usernames

Block the usernames “admin2” and “superuser”.

add_filter('illegal_user_logins', 'block_additional_usernames');

function block_additional_usernames($usernames) {
    $usernames[] = 'admin2';
    $usernames[] = 'superuser';
    return $usernames;
}

Disallow usernames containing specific words

Disallow usernames that contain “admin” or “test”.

add_filter('illegal_user_logins', 'disallow_specific_words');

function disallow_specific_words($usernames) {
    foreach ($usernames as $username) {
        if (strpos($username, 'admin') !== false || strpos($username, 'test') !== false) {
            $disallowed_usernames[] = $username;
        }
    }
    return $disallowed_usernames;
}

Allow all usernames except for “admin”

Allow all usernames except for “admin”.

add_filter('illegal_user_logins', 'allow_all_except_admin');

function allow_all_except_admin($usernames) {
    return array('admin');
}

Remove all disallowed usernames

Allow all usernames by returning an empty array.

add_filter('illegal_user_logins', 'remove_disallowed_usernames');

function remove_disallowed_usernames($usernames) {
    return array();
}

Load disallowed usernames from a text file

Load disallowed usernames from a text file called “blocked_usernames.txt”.

add_filter('illegal_user_logins', 'load_disallowed_usernames_from_file');

function load_disallowed_usernames_from_file($usernames) {
    $blocked_usernames = file('blocked_usernames.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    return array_merge($usernames, $blocked_usernames);
}