• Resolved cimot456

    (@cimot456)


    I created a registration form as below:

    First name :
    Last name :
    Mobile number:
    City :
    Email :
    Password :

    Because I didn’t include the username field, the username will be filled in by first name and last name without spaces (cmiiw).

    So far everything is working fine.

    ====== ### ======

    On the back end I’ve created a custom field with the following meta key:

    1. regno_agent
    2. regno_cust
    3. regno_partner

    And this costume field will be filled in by the admin according to their respective roles after the customer has successfully registered/has been approved.

    Example :

    a. Michael > role > um_agent > regno_agent = a123
    b. Samantha > role > customer > regno_cust = c456
    c. Dorris > roles > um_partner > regno_partner = p789

    What I want to ask, how to make this custom field replace the existing username?

    I got the reference to change the username as described below

    Source: https://www.ads-software.com/support/topic/change-username-after-registration-submit-hook/

    However I’m having trouble editing the code to be able to change the username dynamically.

    Idea I have are as follows:

    1. Change the username using the custom field as in the above example if the field has been updated by admin

    2. As long as the costum field is not updated by admin, then leave it as is (username remains first name and last name)

    Thank you.

Viewing 15 replies - 1 through 15 (of 38 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @cimot456

    You can try the following code snippet:

    add_action( 'profile_update', "um_020422_update_profile", 10, 2 );
    function um_020422_update_profile( $user_id, $old_user_data ){
        
      if( current_user_can("manage_options") ){ 
         $new_login = '';
         switch( um_user("role") ){
           case "um_agent":
                $new_login = um_user("regno_agent");
           break;
           case "customer":
                $new_login = um_user("regno_cust");
           break;
           case "um_partner":
                $new_login = um_user("regno_partner");
           break;
         } 
         wp_update_user( ['ID' => $user_id, 'user_login' => $new_login]  );
      }
    }

    Regards,

    Thread Starter cimot456

    (@cimot456)

    hi @champsupertramp

    Thank you for your reply.

    I will test it now.

    Thread Starter cimot456

    (@cimot456)

    Hi @champsupertramp

    I’ve made sure the roles and meta keys are not wrong.

    But unfortunately the code doesn’t work for me.

    Any idea?

    Thank you

    missveronica

    (@missveronicatv)

    @cimot456

    You can try this updated code snippet:

    add_action( 'profile_update', "um_020422_update_profile", 10, 2 );
    function um_020422_update_profile( $user_id, $old_user_data ) {
    
        global $current_user;
    
        if( current_user_can( "manage_options" ) ) {
     
            $new_login = '';
            um_fetch_user( $user_id );
            switch( um_user( "role" ) ) {
                case "um_agent":   $new_login = um_user( "regno_agent" );   break;
                case "customer":   $new_login = um_user( "regno_cust" );    break;
                case "um_partner": $new_login = um_user( "regno_partner" ); break;
            } 
    
            if( !empty( $new_login )) {
                wp_update_user( ['ID' => $user_id, 'user_login' => $new_login]  );
            }
            um_fetch_user( $current_user->ID );
        }
    }
    Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    I will try it now,

    Thank You

    Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    It didn’t work either ??

    Thank you

    missveronica

    (@missveronicatv)

    @cimot456

    Are you updating the regno_X value at the same time?
    Try to do a second “Save”.

    Try also to clear the UM User Cache at UM Dahsboard.

    Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    1. The roles and meta keys are correct.

    2. I saved the above code using the code snippet plugin.

    3. I did a test and signed up as a new customer.
    (name = michael antono and username field not shown on form this)

    4. On the admin page, I see a list of recent users > michael antono

    Then I edited and updated the regno_agent > a123 field and changed the role to agent (default at registration is subscriber)

    5. After updating, the regno_agent field has saved data > a123.
    However, the username (user_login) is still the same/unchanged (firstname.lastname).

    6. I have also added regno agent fields etc on the profile form.
    As admin, I’m trying to change the regno agent (michael) field to a234.
    And the data has been saved, both on the profile page and in the back.
    ( a123 has been change to a234 for make sure updated is working)

    7. I have cleared cache (I am using wp super cache)

    8. I have also done UM User Cache at UM Dahsboard

    And the username remains unchanged.

    What have I done wrong?

    Thank you

    missveronica

    (@missveronicatv)

    @cimot456

    Thanks for a good description of you process.

    I will change the code snippet so it’s updating the username (user_login) when you change the Role from subscriber to agent.

    Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    Thank you for your effort

    Hope you can achieve it

    missveronica

    (@missveronicatv)

    @cimot456

    This code snippet will update username from a text field with meta values:
    regno_agent, regno_cust, regno_partner

    when you change role from subscriber role to an agent role:
    um_agent, customer, um_partner

    add_action( 'set_user_role', "um_020422_update_profile", 10, 3 );
    function um_020422_update_profile( $user_id, $new_role, $old_roles ) {
    
        if( !empty( $old_roles ) && in_array( 'subscriber', $old_roles )) {
            if( !empty( $new_role ) && in_array( $new_role, array( "um_agent", "customer", "um_partner" ))) {
                if( current_user_can( "manage_options" ) ) {
                    
                    $new_login = '';                
                    switch( $new_role ) {
                        case "um_agent":   $new_login = get_user_meta( $user_id, "regno_agent" );   break;
                        case "customer":   $new_login = get_user_meta( $user_id, "regno_cust" );    break;
                        case "um_partner": $new_login = get_user_meta( $user_id, "regno_partner" ); break;
                    } 
    
                    if( !empty( $new_login )) {
                        wp_update_user( ['ID' => $user_id, 'user_login' => $new_login]  );
                        delete_option( "um_cache_userdata_{$user_id}" );
                    }
                }
            }
        }
    }
    Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    I will try it now,

    Thank You

    Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    Still not working either.

    Can you see this > https://prnt.sc/26osequ

    The partner’s job is to offer products to potential customers by providing a referral code.

    When a prospective customer registers (default as a subscriber), the prospective customer is asked to enter a referral code (owned by a partner) and it will be saved in the Subscriber ID field.

    When this subscriber purchases a product, the admin will change the role to customer.

    It’s the same when a subscriber wants to be a partner or agent.

    And when that happens, I want the username to be changed to a unique code according to their respective roles.

    On the front end, both partners and agents can simply share a link to offer a product through their profile page.
    (I have created a product tab on the profile page).

    It looks like this :

    site.com/regno/a123

    site.com/regno/p234

    Currently I use the user id as the permalink base profile.

    But I’m afraid it will be unfair between the agent and partner because the permalink profile is easy to guess.
    (user id = 1, user id =2, …)

    Previously I have also been looking for how to generate a unique user id (alpanumeric) but to no avail.

    That’s why I created an additional field and want this code to replace the username so that potential customers don’t easily guess the permalink profile, whether it belongs to an agent or partner.

    Any idea?

    Thank you

    Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    Revision / additional information:

    Currently :

    site.com/user/12 -> (user id)

    Targets:

    site.com/regno/a123 -> (username)

    Thank you

    missveronica

    (@missveronicatv)

    @cimot456

    I have updated one mistake in this code snippet:

    add_action( 'set_user_role', "um_020422_update_profile", 10, 3 );
    function um_020422_update_profile( $user_id, $new_role, $old_roles ) {
    
        if( !empty( $old_roles ) && in_array( 'subscriber', $old_roles )) {
            if( !empty( $new_role ) && in_array( $new_role, array( "um_agent", "customer", "um_partner" ))) {
                if( current_user_can( "manage_options" ) ) {
                    
                    $new_login = '';                
                    switch( $new_role ) {
                        case "um_agent":   $new_login = get_user_meta( $user_id, "regno_agent", true );   break;
                        case "customer":   $new_login = get_user_meta( $user_id, "regno_cust", true );    break;
                        case "um_partner": $new_login = get_user_meta( $user_id, "regno_partner", true ); break;
                    } 
    
                    if( !empty( $new_login )) {
                        wp_update_user( ['ID' => $user_id, 'user_login' => $new_login]  );
                        delete_option( "um_cache_userdata_{$user_id}" );
                    }
                }
            }
        }
    }
Viewing 15 replies - 1 through 15 (of 38 total)
  • The topic ‘Change username using costume field’ is closed to new replies.