I have created a custom user registration template for my WP website, based on this code: http://www.tutorialstag.com/create-custom-wordpress-registration-page.html. Now I was wondering if it is possible to add the user ID to the username. The reason I want this is that when a user registers, the (new) user is never a duplicate. So when a new user has the same username as an already registered user, no errors are shown, but the new user is created normally by adding the user ID / or maybe some other random number?

Anyone who can help me further? Thanks.

  • when two people log in with the same username, how does the system know which is which?
    – Milo
    Commented May 14, 2013 at 13:35

1 Answer 1


You can't add the ID at registration because there is no ID until after the user has registered. (See a possible way around this near the bottom).

You could tack on the ID after the registration with the user_register hook.

function add_ID_wpse_99390($a) {
  global $wpdb;
  $user = new WP_User($a);
  $wpdb->query("UPDATE {$wpdb->users} SET user_login = CONCAT(user_login,'_',ID) WHERE ID = {$user->ID}");

Notice that is SQL. That is because you aren't supposed to be altering the login name at all. It is also going to be very confusing for your users as they now have a login name that doesn't match the one they chose. They won't be able to login. This is a flaw inherited from your plan to silently alter the login name.

You could hook to sanitize_user prior to creating the user, but you'd need some other value besides the ID.

function add_ID_v2_wpse_99390($login) {
  $uexists = username_exists( $login );
  if (!empty($uexists)) {
    $incr = get_option('_uname_incre');
    $incr = (!empty($incr)) ? $incr + 1 : 1;
    $login = $login.'_'.$incr;
  return $login;

That code uses a value in the options table to increment the login name suffix. Again, I expect this to be confusing for your users, as their username will be different from the one they chose, but in this case the altered username should be included in the registration email (assuming your users pay attention).

You might be able to run a query like...

$wpdb->get_var("SELECT MAX(ID) FROM {$wpdb->users} LIMIT 1");

And use that value plus one for your suffix but I don't know how reliably that would keep your suffix in line with the actual user ID. And it would expose the user ID, which could be a security issue though probably a minor one.

My final take is "don't do this". At best is just going to be confusing. WordPress should offer a warning if someone tries to create a user with a name that is already taken. That user will have to chose another name and problem solved.

  • Thank you very much. This worked and enriched my understanding on the WPfunctions. Thanks again! PS: i do want to use this function, as this is not really about the member/singnup/login functionality but more about keeping track on statistics.. Something like that, I do not want to bother you with the precise purpose;) Anyway thanks!
    – RobbertT
    Commented May 14, 2013 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.