• 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 - 16 through 30 (of 38 total)
  • Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    I’m really sorry, I still can’t get it to work.

    What can I do to help you for make sure I’m on the right track?

    Thank you

    missveronica

    (@missveronicatv)

    @cimot456

    You can try this new updated code snippet which I have tested at my site and it’s updating username now:

    add_action( 'set_user_role', "um_020422_update_profile", 10, 3 );
    function um_020422_update_profile( $user_id, $new_role, $old_roles ) {
        
        global $wpdb;
        
        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 ) && !username_exists( $new_login )) {
                        $wpdb->update( $wpdb->users, 
                                    ['user_login' => sanitize_text_field( $new_login )], 
                                    ['ID' => $user_id] );
    
                        delete_option( "um_cache_userdata_{$user_id}" );
                    }
                }
            }
        }
    }
    Thread Starter cimot456

    (@cimot456)

    Hi @missveronicatv

    Sorry, just replied to the message.

    Unfortunately it still doesn’t work.

    by the way, I tried using the code snippet I found here.

    https://www.ads-software.com/support/topic/ultimate-member-username-edit/

    And it has successfully changed the username as I wanted.
    (regno_agent,etc)

    My question,
    can this change also be made on the admin page?

    /wp-admin/user-edit.php?user_id=19&wp_http_referer=%2Fwp-admin%2Fusers.php

    Thank You

    missveronica

    (@missveronicatv)

    @cimot456

    My latest code works for a role change by selecting a user being a subscriber
    and then using the UM Role change dropdown on WP Users page to change to um_agent, customer, or um_partner.

    It works also from the WP User Edit page.

    All role updates requires that there is a value in the user_meta table to use for the update ie regno_agent, regno_cust, regno_partner are being set a value for the new role.

    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    Hi sorry I just replied to the message.

    But how do I make the code work?

    I’ve looked time and time again regarding setting roles and meta field values.
    And so far I see no error.

    One proof is that when I change the value of a meta field, it is always updated and saved.
    So is the role setting.

    I’m saving the code via a code snippet plugin.

    So, what am I missing here?
    And what can I do to make sure I’m on the right track?

    Thank You

    missveronica

    (@missveronicatv)

    @cimot456

    Try this code snippet with the addition of error handling.

    Errors are displayed on top of the WP Users or WP Edit User page with a red margin.

    Two error messages are possible to get:
    ERROR: Empty meta_value for this role: customer or this username exists: A123C
    ERROR: You are not allowed to manage this option.

    add_action( 'set_user_role', "um_020422_update_profile", 10, 3 );
    add_action( 'admin_notices', 'um_020422_error_message', 10 );
    
    function um_020422_update_profile( $user_id, $new_role, $old_roles ) {
        
        global $wpdb;
        
        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 ) && !username_exists( $new_login )) {
                        $wpdb->update( $wpdb->users, 
                                    ['user_login' => sanitize_text_field( $new_login )], 
                                    ['ID' => $user_id] );
    
                        delete_option( "um_cache_userdata_{$user_id}" );
    
                    } else {
                        $error = new WP_Error( '020422', 'Empty meta_value for this role: ' . $new_role . ' or this username exists: ' . $new_login );
                        set_transient("my_um_020422_update_profile_errors", $error, 30);
                    }
    
                } else {
                    $error = new WP_Error( '020422', 'You are not allowed to manage this option.' );
                    set_transient("my_um_020422_update_profile_errors", $error, 30);
                }
            }
        }
    }
    
    function um_020422_error_message() {
    
        if ( $error = get_transient( "my_um_020422_update_profile_errors" ) ) { ?>
            <div class="error">
                <p>ERROR: <?php echo $error->get_error_message(); ?></p>
            </div><?php
        
            delete_transient( "my_um_020422_update_profile_errors" );
        }
    }
    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    Hi, sorry I just replied to the message.

    Since last night (our local time) I’m having trouble accessing the site (can’t do anything about it).

    After asking the hosting company, they upgraded their security system and don’t know when the site will be back to normal.

    If you don’t mind, can you wait to hear from me in the next few days for the latest code results?

    Thank You

    missveronica

    (@missveronicatv)

    @cimot456

    I have made additional error tests on the MySQL Update
    which you can download from this Gist:

    https://gist.github.com/MissVeronica/c87736583d29f6613e8257ca4e4e72f2

    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    I just was only able to access the site today, so I can only give news now.

    Here is a summary of what I have done:

    0). When the site is back online, there is a notification of a plugin update, one of which is the ultimate member plugin > and I have updated it

    1). Every different or new code snippet, I test and register as a new user.

    And as explained earlier, the default role you get is subscriber.

    So I go to users tab > new/last user > edit > and only updated the required field values ??(example : agent > regno_agent).

    The data has been saved successfully.

    I also edited some of the other users to see the changes.

    — Up to this point there has been no change (username).—

    2). I never declared that every time I update a field in user information, the user role automatically changes to no role and I really don’t know why that is.

    Description :

    * new user > default role : subscriber > after updated field > final role : no role
    * existing user > last role : customer > after updated field > final role : no role

    I tried updating again on the same page (page > /wp-admin/user-edit.php?user_id=17&wp_http_referer=%2Fwp-admin%2Fusers.php%3Fupdate%3Dpromote) but the role remains empty (no role).

    But when I update via page > /wp-admin/users.php > it manages to change roles.

    * final role : no role > newest role : Agent
    * final role : no role > newest role : Customer

    screenshots

    — Up to this point there has been no change (username). Although roles and fields have been updated. —

    3). Problems:

    No matter how many times a new user comes in, how many times it is updated, I fixed can’t get the code to work.

    Because of what?

    because the code has a function > If the admin updates the user regno field, AND changes the roles status (default as subscriber) to agent/partner/customer, then change the username taken from the regno field.

    Meanwhile, what I have done so far is as described in point 2 > description :

    “subscriber > update data > no role > agent”, >>> this is not eligible.(Please correct me if I’m wrong)

    But when I try another action (accidentally):

    “subscriber > update data > no role > (back to) subscriber > agent >>> username has been updated.

    In other words the last code you created has worked fine.

    Correction: It’s possible that the previous codes would also work fine if I was aware of this action, but I haven’t tested it yet ??

    So, I want to say a especially to @missveronicatv really thank you for take the time to help me achieve this.

    I hope this explanation is easy to understand.

    Regards

    missveronica

    (@missveronicatv)

    @cimot456

    Yes you still have one issue not resolved:

    “subscriber > update data > no role > agent”, >>> this is not eligible.(Please correct me if I’m wrong)

    But when I try another action (accidentally):

    “subscriber > update data > no role > (back to) subscriber > agent >>> username has been updated.

    Do you have another code snippet involved in the updating of the agent data for the subscribers?

    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    Yes you still have one issue not resolved:

    If you can improve it, it will only make my smile wider ??

    === ## ===

    Do you have another code snippet involved in the updating of the agent data for the subscribers?

    Yes, I have some other snippets like ;

    1. https://www.ads-software.com/support/topic/ultimate-member-username-edit/

    Status : Inactive

    Description: Plan B for this question.

    2. https://github.com/MissVeronica/UM-Mobile-Number-Login

    Status : active

    Remarks : Make uniform entry access via mobile number.

    3. https://bavotasan.com/2009/adding-extra-fields-to-the-wordpress-user-profile/

    status : active

    Description : Adding the required extra fields such as regno, mobile_number, until the full address..

    For some reason I do not ask users to fill in their detailed information on the registration form but when they update status/role.

    And after that, customers or agent are only allowed to change their nickname and whatsapp number on their profile page, the rest can only be seen and accessed by the admin.

    So far I am updating regno based on the code in point 3.

    screenshot
    page = /wp-admin/user-edit.php?user_id=24&wp_http_referer=%2Fwp-admin%2Fusers.php

    The rest of the snippets like creating a custom tab on the profile page, redirect after login, displaying the costume field on the admin page, etc.

    Let me know if you need anything.

    Really thank you

    missveronica

    (@missveronicatv)

    @cimot456

    The number 3 code snippet try to add an UM remove cache to refresh the updated fields in the UM cache.

    Add after your last update_user_meta add:

            UM()->user()->remove_cache( $user_id );
            um_fetch_user( $user_id );
    Thread Starter cimot456

    (@cimot456)

    Ok wait please…

    Thread Starter cimot456

    (@cimot456)

    @missveronicatv

    1. I have added the above code as you can see here.

    2. I don’t see any changes and it’s still the same as before.

    Recent tests:

    1. New user > subscriber > Update regno & change role > empty role (no role) but regno is saved.

    Action to achieve it : empty role > subscriber > agent > username updated.

    2. New user > Subscriber > Change role only > Role updated & showing message ERROR: Empty meta_value for this role: um_agent or this username exists:
    Then when I updated the regno_agent > role field returned to empty & the regno agent was saved.
    Action to achieve it : empty role > subscriber > agent > username updated.

    3. New users > Subscribers > Only change regno_agent > (then the same as in point 1).

    Conclusion :

    All actions in points 1,2,3 end in the role status to empty (no role) and to achieve username can be updated must go through the following actions:

    when the role position is empty (no role) > change role to subscriber > then change role to agent > username will be updated.

    Thank you

    missveronica

    (@missveronicatv)

    @cimot456

    Can you post here in the Forum all Field Names being updated with the update_user_meta in the code snippet 3.

Viewing 15 replies - 16 through 30 (of 38 total)
  • The topic ‘Change username using costume field’ is closed to new replies.