Stop user registration on wordpress websites

Solution:

If you are comfortable with editing your theme’s function.php then these scripts will help:

Disable all user registrations:

If your site is not handling any memberships and you want to totally disable the user registrations, then you can add this script in your active themes function.php file:

add_filter( 'wp_pre_insert_user_data', 'disable_user_registrations', 10, 2 );
function disable_user_registrations ($data, $update) {
    if( !$update ) {
        return [];
    }
    return $data;
}

This will throw an error: ‘Not enough data to create this user.’

Disable specific user roles:

For example, disable user registrations with administrator role:

add_filter( 'wp_pre_insert_user_data', 'disable_user_registrations_for_role', 10, 4 );
function disable_user_registrations_for_role ($data, $update, $userId, $userdata) {
    if( !$update && !empty($userdata['role']) && $userdata['role'] == 'administrator' ) ) {
        return [];
    }

    return $data;
}

Disable specific usernames:

If you would like to disable registrations with specific usernames such as admin, then you can use below code:

add_filter( 'illegal_user_logins', 'disable_user_registrations_with_usernames' );
function disable_user_registrations_with_usernames () {
    return ['admin'];  //disables anyone registering with the username admin
}