Skip to content

Instantly share code, notes, and snippets.

@philipnewcomer
Last active February 11, 2023 19:05
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save philipnewcomer/59a695415f5f9a2dd851deda42d0552f to your computer and use it in GitHub Desktop.
Save philipnewcomer/59a695415f5f9a2dd851deda42d0552f to your computer and use it in GitHub Desktop.
Generate a unique username in WordPress
<?php
/**
* Recursive function to generate a unique username.
*
* If the username already exists, will add a numerical suffix which will increase until a unique username is found.
*
* @param string $username
*
* @return string The unique username.
*/
function generate_unique_username( $username ) {
static $i;
if ( null === $i ) {
$i = 1;
} else {
$i++;
}
if ( ! username_exists( $username ) ) {
return $username;
}
$new_username = sprintf( '%s-%s', $username, $i );
if ( ! username_exists( $new_username ) ) {
return $new_username;
} else {
return call_user_func( __FUNCTION__, $username );
}
}
@mronikoyi
Copy link

thanks @brian-stinar !
I thought so too!

@mronikoyi
Copy link

I didn't knbow about the boilerplate plugin generator! Thanks for the info !

@brian-stinar
Copy link

Sure, no problem. We make a good chunk of cash off open source software, and I try and contribute how I can.

This would actually be a good plugin to write... Maybe I'll have one of my junior level people take care of this.

@mharis
Copy link

mharis commented Sep 23, 2022

Added comment block.

/**
 * Generates a unique username.
 *
 * @param string $username Username to check.
 * @return string username
 */
function generate_unique_username( $username ) {
	$username = sanitize_user( $username );

	static $i;
	if ( null === $i ) {
		$i = 1;
	} else {
		$i ++;
	}
	if ( ! username_exists( $username ) ) {
		return $username;
	}
	$new_username = sprintf( '%s-%s', $username, $i );
	if ( ! username_exists( $new_username ) ) {
		return $new_username;
	} else {
		return call_user_func( __FUNCTION__, $username );
	}
}

@TinkerPaul
Copy link

TinkerPaul commented Oct 15, 2022

New here on GitHub but I thought I'd share my take on this code. My website does not allow user registration. Users are currently created manually. This will allow for use of a csv file (via plugin) to load new users and ensure unique usernames. I replaced the recursive function calls with a while loop. Because of where I hooked into sanitize_user is already performed by the .../includes/user.php edit_user function. Because of hook location removal of the registration error is required.

function generate_unique_username( $errors, $update, $user ) {
        $username = $user->user_login;
        //strip off any trailing digits on the username (Optional, delete if functionality not desired)
        static $loop = 0;
        do {
                $loop++;
                $last = substr($username, strlen($username)-$loop);
        } while ( is_numeric( $last ) );
        $username = substr($username, 0, strlen($username)-($loop-1));

        //iterate to find the next unused username
        $i = 0;
        $new_username = $username;
        while ( username_exists( $new_username ) ) {
                $i ++;
                $new_username = sprintf( '%s%d', $username, $i );
        }

        //Store the new username and clear the error log for username error only
        $user->user_login = $new_username;
        if ($errors->has_errors()) {
                if (in_array('user_login', $errors->get_error_codes()) ) {
                        if(count($errors->get_error_messages( 'user_login' )) == 1) {
                                if(str_contains($errors->get_error_message( 'user_login' ), "This username is already registered")) {
                                        $errors->remove('user_login');
                                }
                        }
                }
        }
}

add_action( 'user_profile_update_errors' , 'generate_unique_username', 10 , 3 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment