• Resolved w-sky

    (@w-sky)


    Hello, only for the reason to make the registration form easier to use, I want to remove or hide the user name field from the registration form. But then a user name has to be created automatically. Is this possible?

    It would be nice to have simple user names, for example, first letter of first name + dot + last name. (+ a counting number if a name already exists).

    Or maybe simply user name = email address, if that is allowed in WP? The page https://docs.ultimatemember.com/article/87-adding-a-registration-form says that I must add either the username field or the username/email field to the registration form. Does this mean, when I use the latter, that the user names of the registrants will be same as email address?

Viewing 2 replies - 1 through 2 (of 2 total)
  • @w-sky

    You can try this code snippet making usernames like: “a.smith” and “a.smith625”:

    add_filter( 'um_add_user_frontend_submitted', 'um_autocreate_username', 10, 1 );
    
    function um_autocreate_username( $args ) {
    
        if( $args['mode'] == 'register' ) {
            if( function_exists( 'mb_substr' )) {
                $first_name_char = mb_substr( $args['first_name'], 0, 1 );
                $username = mb_strtolower( sanitize_text_field( $first_name_char . '.' . $args['last_name'] ));
            } else {
                $first_name_char = substr( $args['first_name'], 0, 1 );
                $username = strtolower( sanitize_text_field( $first_name_char . '.' . $args['last_name'] ));
            }
            
            $args['user_login'] = $username;
            while( username_exists( $args['user_login'] )) {
                $args['user_login'] = $username . rand( 100, 999 );
            }
        }
        return $args;
    }

    Install the code snippet to your active theme’s functions.php file or use the “Code Snippets” plugin.

    https://ww.wp.xz.cn/plugins/code-snippets/

    Plugin Support andrewshu

    (@andrewshu)

    Hi @w-sky

    This thread has been inactive for a while so we’re going to go ahead and mark it Resolved.

    Please feel free to re-open this thread if any other questions come up and we’d be happy to help. 🙂

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Auto create user name at registration’ is closed to new replies.