• Resolved anilreddy0102

    (@anilreddy0102)


    I would like to create an alphanumeric username with ABCD as prefix and a 5 digit number (ABCD-23456). This should be created and emailed to the user once the user verifies his email by clicking on the verification link sent to him post registration. Or this can also be created once the user submits the registration form. Pls help

Viewing 4 replies - 1 through 4 (of 4 total)
  • @anilreddy0102

    this can also be created once the user submits the registration form

    This code snippet will create your prefixed username with 5 random digits at Registration.

    Add the {username} email placeholder to the Email Activation email template.

    https://docs.ultimatemember.com/article/1340-placeholders-for-email-templates

    add_filter( 'um_add_user_frontend_submitted', 'my_custom_auto_create_username', 10, 2 );
    
    	function my_custom_auto_create_username( $args, $form_data ) {
    
    		if( $args['mode'] == 'register' ) {
    			$prefix = 'ABCD-';
    			$args['user_login'] = $prefix . rand( 10000, 99999 );
    			while( username_exists( $args['user_login'] )) {
    				$args['user_login'] = $prefix . rand( 10000, 99999 );
    			}
    		}
    		return $args;
    	}

    You install the code snippet by adding it
    to your active theme’s functions.php file
    or use the “Code Snippets” Plugin

    https://www.ads-software.com/plugins/code-snippets/

    • This reply was modified 3 months, 2 weeks ago by missveronica.
    Thread Starter anilreddy0102

    (@anilreddy0102)

    Thanks for the prompt reply. I have added the code to my theme’s functions.php file. When I submit the form, username is automatically using the first name and last name. It’s not working as expected. Please help. FYI, I didn’t add the username field to the form. My form has name, surname, email, mobile and password fields.

    @anilreddy0102

    You are right the old code snippet did nothing with the username.
    You can try this code snippet instead

    add_filter( 'um_add_user_frontend_submitted', 'my_custom_auto_create_username', 10, 2 );
    
    function my_custom_auto_create_username( $args, $form_data ) {
    
        if ( isset( $form_data['mode'] ) && $form_data['mode'] == 'register' ) {
            $prefix = 'ABCD-';
            $args['user_login'] = $prefix . rand( 10000, 99999 );
            while( username_exists( $args['user_login'] )) {
                $args['user_login'] = $prefix . rand( 10000, 99999 );
            }
        }
        return $args;
    }
    Thread Starter anilreddy0102

    (@anilreddy0102)

    This worked. Thanks a lot. Hope this will continue working even with future plugin updates as well?

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.