• Resolved tonnick0033

    (@tonnick0033)


    Hello

    I added a register form with only first name / lastname / email. Seems that username is generated from email first par (before @)

    But how can we generate a username with “firstname-lastname” instead of “emailfirstpart”

    thx !

Viewing 14 replies - 1 through 14 (of 14 total)
  • Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi @tonnick0033,

    The user registration plugin has a specific field for the user name and display name. You can see it in this screenshot https://prnt.sc/FsyccppWDa1-.
    If you include this field, users can enter usernames or display names of their choice. We have added these fields to user registration because users always want to show the profile names based on their needs.

    If you’re already familiar with these features but still want to generate a user name from the first and last names, try the code below.

    add_action( 'user_registration_after_register_user_action', 'ur_insert_nickname', 1, 3 );
    function ur_insert_nickname( $valid_form_data, $form_id, $user_id ) {
        $username = isset( $valid_form_data['user_login'] ) ? $valid_form_data['user_login']->value : '';
        wp_update_user([
    'ID' => $user_id, // this is the ID of the user you want to update.
    'first_name' => $username,
    'last_name' => $username,
    ]);
    }

    If you do not know where to custom code snippets on your site, please check this documentation https://docs.wpeverest.com/user-registration/docs/how-to-add-the-custom-code-snippet-on-your-site/.

    Regards!

    Thread Starter tonnick0033

    (@tonnick0033)

    this is not working, the first name and last name are now empty and login is still the first part fof email.

    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi @tonnick0033,

    The code was well tested before providing to you. The same code snippet works fine for other users, so it is impossible if the code does not work for you.

    Maybe you have added code not appropriately or doing something wrong. So please provide us a screenshot of how you added the code snippet on your site and the registration form link. We will look into it and get back to you accordingly.

    Regards!

    Thread Starter tonnick0033

    (@tonnick0033)

    code has been added in function.php

    https://paste.pics/5a7b4efcda560b2d6aca05926cbae1ab

    I comented it as it’s not working

    registration link is here : https://dev.chadeque.fr/sinscrire/

    there are 2 forms as each form is linked to differents roles.

    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi @tonnick0033,

    Thank you for writing back,

    Please try the following code snippet.

    add_action( 'user_registration_after_user_meta_update', 'ur_insert_username', 1, 3 ); 
    function ur_insert_username( $valid_form_data, $form_id, $user_id ) { 
        global $wpdb; 
        $firstname = isset( $valid_form_data['first_name'] ) ? $valid_form_data['first_name']->value : ''; 
        $lastname = isset( $valid_form_data['last_name'] ) ? $valid_form_data['last_name']->value : ''; 
        $custom_username =  $firstname . "-" . $lastname; 
        $user = get_user_by('login', $custom_username); 
        
        if( ! empty( $user ) ) { 
            $custom_username =  $custom_username ."-" . $user_id; 
        } 
        $wpdb->update( 
            $wpdb->users, 
            ['user_login' => $custom_username], 
            ['ID' => $user_id] 
        ); 
    }

    Regards!

    Thread Starter tonnick0033

    (@tonnick0033)

    great ! this is working much better ! But last point. I set up a redirection with auto login and auto approval. Seems that with this code, the auto login is not working anymore. the redirection is working, but user is not login.
    Do we have to add custom code here as well ?

    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    Hi @tonnick0033,

    The provided code does not relate to redirection and user login options. Please provide us a screenshot of your redirection configuration; we will look into it and get back to you accordingly.

    Regards!

    Thread Starter tonnick0033

    (@tonnick0033)

    settings used are as following :

    https://paste.pics/ead349b8ee7b85f6974c5b2ce6597260

    this works perfectly without the code you provided. but once the code added, the auto login and redirect are not working anymore.

    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    HI @tonnick0033,

    We registered on your site and checked the redirection but did not find any issue. The redirection is working fine. For your confirmation, check this screen record https://www.awesomescreenshot.com/video/11418994?key=778b635918cefe62ef027e0847e0b05a

    Regards!

    Thread Starter tonnick0033

    (@tonnick0033)

    o sorry my mistake. I deployed the code on live website. this is why it’s still working on dev website.

    I added the code as well on dev, and the auto login / redirect are not working anymore on dev website.

    I deleted your test account, you can test it again.

    Plugin Support Amrit Kumar Shrestha

    (@shresthauzwal)

    HI @tonnick0033,

    We have tested the registration form, and the redirection works fine. We suggest you reconfirm from your end.

    Regards!

    Thread Starter tonnick0033

    (@tonnick0033)

    the redirection is working but auto login is not. you are not login once you register.

    Thread Starter tonnick0033

    (@tonnick0033)

    hello
    do you have an update about this problem ? how can we make working together your code and the redirection + auto login option ?

    thx.

    Thread Starter tonnick0033

    (@tonnick0033)

    Sorry to disturb you, but this would be great if this function would not prevent the auto-login option from your own plugin.
    Do you think you’ll find some solution here ?
    thx.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘user first and last name for username’ is closed to new replies.