I need to auto-generate usernames upon WordPress registration. I have a custom registration form set up and would like to create the username in functions.php.

Can someone tell me why this isn't working? It seems like it should work after reading the WordPress Codex on customized registration forms?

function register_hook ( $user_id ) {
  update_user_meta( $user_id, 'signup_username', 'the-auto-generated-name' );
  // also tried this:
  // update_user_meta( $user_id, 'user_login', 'the-auto-generated-name' );
add_action( 'user_register', 'register_hook' );
  • do you mean the user login name? that's not stored in user meta, it's a column in the users table.
    – Milo
    Nov 5, 2014 at 18:15
  • yes, that's what i mean.
    – user1462
    Nov 5, 2014 at 18:19

2 Answers 2


One alternative is to modify the $_POST['user_login'] input value when submitting new registration form, that is before WP process the registration form. A good hook to achieve this is login_form_register that fires before processing and rendering registration form. login_init also works but need more work to make sure we are on register action.

add_action('login_form_register', 'custom_user_login');
function custom_user_login() {

    // make sure regisration form is submitted

    // base of user_login, change it according to ur needs
    $ulogin = 'random-user';

    // make user_login unique so WP will not return error
    $check = username_exists($ulogin);
    if (!empty($check)) {
        $suffix = 2;
        while (!empty($check)) {
            $alt_ulogin = $ulogin . '-' . $suffix;
            $check = username_exists($alt_ulogin);
        $ulogin = $alt_ulogin;

    $_POST['user_login'] = $ulogin;
  • Hi Ahmad, thanks for your response. I tried out your code and removed the "signup_username" field from my registration form, but WP keeps returning the registration form and not completing because the 'signup_username' is empty. I also tried updating the second to last line in your code from 'user_login' to 'signup_username', but it didn't make a difference. Any ideas?
    – user1462
    Nov 5, 2014 at 19:53
  • Are you using default wp-login.php?action=register or a custom URL?
    – Ahmad M
    Nov 5, 2014 at 20:03
  • A custom url -- /register
    – user1462
    Nov 5, 2014 at 20:05
  • The concept is the same, but you might need to update the code above to overwrite whatever $_POST value passed to register_new_user() or wp_insert_user() as the user login name. Also the hooks mentioned above only works on wp-login.php, so you will need to change it to a hook that fires before processing the form, init or after_setup_theme might work depending on at what point the form is processed.
    – Ahmad M
    Nov 5, 2014 at 20:12

You can alter user login via the pre_user_login filter. Note that this runs when the user is created or updated.

function wpd_custom_user_login( $user_login ) {
    $user_login = 'the-auto-generated-name';
    return $user_login;
add_filter( 'pre_user_login' , 'wpd_custom_user_login' );
  • Thanks for the help! I just need to auto generate the username one time -- right after they successfully complete the registration form. How can it be made to only run then? Is the user_register hook correct?
    – user1462
    Nov 5, 2014 at 18:22
  • You can hook the register_post action to add your filter right before new users are registered.
    – Milo
    Nov 5, 2014 at 18:45

Your Answer

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

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