How to override WordPress registration and insert an auto-generated username?

The question:

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' );

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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;

Method 2

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' );

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment