Change username using costume field
-
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_partnerAnd 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 = p789What 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://ww.wp.xz.cn/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.
-
Yes, with pleasure.
add_action( 'show_user_profile', 'extra_user_profile_fields' ); add_action( 'edit_user_profile', 'extra_user_profile_fields' ); add_action( "user_new_form", "extra_user_profile_fields" ); function extra_user_profile_fields( $user ) { ?> <h3><?php _e("Additional Information", "blank"); ?></h3> <table class="form-table"> <tr> <th><label for="regno_admin"><?php _e("ID Admin"); ?></label></th> <td> <input type="text" name="regno_admin" id="regno_admin" value="<?php echo esc_attr( get_the_author_meta( 'regno_admin', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="regno_partner"><?php _e("ID Partner"); ?></label></th> <td> <input type="text" name="regno_partner" id="regno_partner" value="<?php echo esc_attr( get_the_author_meta( 'regno_partner', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="regno_subscriber"><?php _e("ID Subscriber"); ?></label></th> <td> <input type="text" name="regno_subscriber" id="regno_subscriber" value="<?php echo esc_attr( get_the_author_meta( 'regno_subscriber', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="regno_customer"><?php _e("ID Customer"); ?></label></th> <td> <input type="text" name="regno_customer" id="regno_customer" value="<?php echo esc_attr( get_the_author_meta( 'regno_customer', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="regno_owner"><?php _e("ID Owner"); ?></label></th> <td> <input type="text" name="regno_owner" id="regno_owner" value="<?php echo esc_attr( get_the_author_meta( 'regno_owner', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="regno_owner_refferal"><?php _e("ID Owner Refferal"); ?></label></th> <td> <input type="text" name="regno_owner_refferal" id="regno_owner_refferal" value="<?php echo esc_attr( get_the_author_meta( 'regno_owner_refferal', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="regno_reseller"><?php _e("ID Reseller"); ?></label></th> <td> <input type="text" name="regno_reseller" id="regno_reseller" value="<?php echo esc_attr( get_the_author_meta( 'regno_reseller', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="regno_agent"><?php _e("ID Agent"); ?></label></th> <td> <input type="text" name="regno_agent" id="regno_agent" value="<?php echo esc_attr( get_the_author_meta( 'regno_agent', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="regno_agent_refferal"><?php _e("ID Agent Refferal"); ?></label></th> <td> <input type="text" name="regno_agent_refferal" id="regno_agent_refferal" value="<?php echo esc_attr( get_the_author_meta( 'regno_agent_refferal', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="role"><?php _e("Roles"); ?></label></th> <td> <input type="text" name="role" id="role" value="<?php echo esc_attr( get_the_author_meta( 'role', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="first_name"><?php _e("First Name"); ?></label></th> <td> <input type="text" name="first_name" id="first_name" value="<?php echo esc_attr( get_the_author_meta( 'first_name', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="last_name"><?php _e("Last Name"); ?></label></th> <td> <input type="text" name="last_name" id="last_name" value="<?php echo esc_attr( get_the_author_meta( 'last_name', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="full_name"><?php _e("Full Name"); ?></label></th> <td> <input type="text" name="full_name" id="full_name" value="<?php echo esc_attr( get_the_author_meta( 'full_name', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="user_login"><?php _e("Username"); ?></label></th> <td> <input type="text" name="user_login" id="user_login" value="<?php echo esc_attr( get_the_author_meta( 'user_login', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="nickname"><?php _e("Nickname"); ?></label></th> <td> <input type="text" name="nickname" id="nickname" value="<?php echo esc_attr( get_the_author_meta( 'nickname', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <?php /*) <tr> <th><label for="gender"><?php _e("Gender"); ?></label></th> <td> <input type="text" name="gender" id="gender" value="<?php echo esc_attr( get_the_author_meta( 'gender', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr>*/?> <tr> <th><label for="gender">Gender</label></th> <td> <select name="gender" id="gender" > <option value="male" <?php selected( 'Male', get_the_author_meta( 'gender', $user->ID ) ); ?>>Male</option> <option value="female" <?php selected( 'Female', get_the_author_meta( 'gender', $user->ID ) ); ?>>Female</option> </select> </td> </tr> <tr> <th><label for="dob"><?php _e("Date Of Birthday"); ?></label></th> <td> <input type="text" name="dob" id="dob" value="<?php echo esc_attr( get_the_author_meta( 'dob', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="address"><?php _e("Address"); ?></label></th> <td> <input type="text" name="address" id="address" value="<?php echo esc_attr( get_the_author_meta( 'address', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="sub-district"><?php _e("Subdistrict"); ?></label></th> <td> <input type="text" name="sub-district" id="sub-district" value="<?php echo esc_attr( get_the_author_meta( 'sub-district', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="district"><?php _e("District"); ?></label></th> <td> <input type="text" name="district" id="district" value="<?php echo esc_attr( get_the_author_meta( 'district', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="cities"><?php _e("Cities"); ?></label></th> <td> <input type="text" name="cities" id="cities" value="<?php echo esc_attr( get_the_author_meta( 'cities', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="provinces"><?php _e("Provinces"); ?></label></th> <td> <input type="text" name="provinces" id="provinces" value="<?php echo esc_attr( get_the_author_meta( 'provinces', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="zip_code"><?php _e("Zipcode"); ?></label></th> <td> <input type="text" name="zip_code" id="zip_code" value="<?php echo esc_attr( get_the_author_meta( 'zip_code', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="mobile_number"><?php _e("No Telp"); ?></label></th> <td> <input type="text" name="mobile_number" id="mobile_number" value="<?php echo esc_attr( get_the_author_meta( 'mobile_number', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="whatsapp"><?php _e("Whatsapp"); ?></label></th> <td> <input type="text" name="whatsapp" id="whatsapp" value="<?php echo esc_attr( get_the_author_meta( 'whatsapp', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="accountnumb"><?php _e("Account"); ?></label></th> <td> <input type="text" name="accountnumb" id="accountnumb" value="<?php echo esc_attr( get_the_author_meta( 'accountnumb', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="bank"><?php _e("Bank"); ?></label></th> <td> <input type="text" name="bank" id="bank" value="<?php echo esc_attr( get_the_author_meta( 'bank', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="accountname"><?php _e("Account Name"); ?></label></th> <td> <input type="text" name="accountname" id="accountname" value="<?php echo esc_attr( get_the_author_meta( 'accountname', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="buktab"><?php _e("Cover Bank"); ?></label></th> <td> <input type="text" name="buktab" id="buktab" value="<?php echo esc_attr( get_the_author_meta( 'buktab', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="ktpmtr"><?php _e("ID Card Partner "); ?></label></th> <td> <input type="text" name="ktpmtr" id="ktpmtr" value="<?php echo esc_attr( get_the_author_meta( 'ktpmtr', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="profile_photo"><?php _e("URL Foto"); ?></label></th> <td> <input type="text" name="profile_photo" id="profile_photo" value="<?php echo esc_attr( get_the_author_meta( 'profile_photo', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="instagram"><?php _e("Instagram"); ?></label></th> <td> <input type="text" name="instagram" id="instagram" value="<?php echo esc_attr( get_the_author_meta( 'instagram', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> <tr> <th><label for="facebook"><?php _e("Facebook"); ?></label></th> <td> <input type="text" name="facebook" id="facebook" value="<?php echo esc_attr( get_the_author_meta( 'facebook', $user->ID ) ); ?>" class="regular-text" /><br /> <span class="description"><?php _e(""); ?></span> </td> </tr> </table> <?php } add_action( 'personal_options_update', 'save_extra_user_profile_fields' ); add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' ); add_action( 'user_register', 'save_extra_user_profile_fields' ); function save_extra_user_profile_fields( $user_id ) { if ( !current_user_can( 'edit_user', $user_id ) ) { return false; } update_user_meta( $user_id, 'regno_admin', $_POST['regno_admin'] ); update_user_meta( $user_id, 'regno_partner', $_POST['regno_partner'] ); update_user_meta( $user_id, 'regno_subscriber', $_POST['regno_subscriber'] ); update_user_meta( $user_id, 'regno_customer', $_POST['regno_customer'] ); update_user_meta( $user_id, 'regno_owner', $_POST['regno_owner'] ); update_user_meta( $user_id, 'regno_owner_refferal', $_POST['regno_owner_refferal'] ); update_user_meta( $user_id, 'regno_reseller', $_POST['regno_reseller'] ); update_user_meta( $user_id, 'regno_agent', $_POST['regno_agent'] ); update_user_meta( $user_id, 'regno_agent_refferal', $_POST['regno_agent_refferal'] ); update_user_meta( $user_id, 'first_name', $_POST['first_name'] ); update_user_meta( $user_id, 'last_name', $_POST['last_name'] ); update_user_meta( $user_id, 'full_name', $_POST['full_name'] ); update_user_meta( $user_id, 'user_login', $_POST['user_login'] ); update_user_meta( $user_id, 'nickname', $_POST['nickname'] ); update_user_meta( $user_id, 'gender', $_POST['gender'] ); update_user_meta( $user_id, 'dob', $_POST['dob'] ); update_user_meta( $user_id, 'address', $_POST['address'] ); update_user_meta( $user_id, 'sub-district', $_POST['sub-district'] ); update_user_meta( $user_id, 'district', $_POST['district'] ); update_user_meta( $user_id, 'cities', $_POST['cities'] ); update_user_meta( $user_id, 'provinces', $_POST['provinces'] ); update_user_meta( $user_id, 'zip_code', $_POST['zip_code'] ); update_user_meta( $user_id, 'mobile_number', $_POST['mobile_number'] ); update_user_meta( $user_id, 'whatsapp', $_POST['whatsapp'] ); update_user_meta( $user_id, 'accountnumb', $_POST['accountnumb'] ); update_user_meta( $user_id, 'bank', $_POST['bank'] ); update_user_meta( $user_id, 'accountname', $_POST['accountname'] ); update_user_meta( $user_id, 'buktab', $_POST['buktab']); update_user_meta( $user_id, 'ktpmtr', $_POST['ktpmtr'] ); update_user_meta( $user_id, 'profile_photo', $_POST['profile_photo']); update_user_meta( $user_id, 'instagram', $_POST['instagram'] ); update_user_meta( $user_id, 'facebook', $_POST['facebook'] ); UM()->user()->remove_cache( $user_id ); um_fetch_user( $user_id ); }After looking at your code snippet I have two comments.
1. Remove the HTML input statement displaying the User Role.
Replace the HTML input with a Role display for information only.There is no code in the update function taking care of any entered value for the Role and there is a possible conflict issue where WP code or plugin code may take action on the $_POST[‘role’] content and clear the value.
2.
user_logincan’t be updated by a call to the WPupdate_user_metafunction.You can remove both the HTML input statement with
user_loginand theupdate_user_metacall to updateuser_login. This code updating withupdate_user_metawill probably add the meta_key to the WP usermeta table and there is a possible conflict with the WP users table and it’s real value ofuser_login. I recommend you to remove anyuser_logincontent in the usermeta table.My code snippet adds the security of only updating
user-loginwhen there is a role change fromsubscriberto an agent/customer role.Latest version of my code snippet:
https://gist.github.com/MissVeronica/c87736583d29f6613e8257ca4e4e72f2
-
This reply was modified 4 years, 2 months ago by
missveronica.
Finally…
No more empty roles, no more extra effort,
Perfect!You have done it…
Now, I smile broadly:)
really really thank you @missveronicatv
-
This reply was modified 4 years, 2 months ago by
cimot456.
Thank you for your feedback.
One note about the second code snippet “Mobile Number Login”:
I have made an addition of custom validation of the mobile number when used in the UM Profile and Registration Forms. Make sure that you have the latest code snippet.
You can validate that the text field contains an “unique mobile number”.
The default validation options in UM are either “mobile number” or “unique value”.
Download from:
https://github.com/MissVeronica/UM-Mobile-Number-Login-
This reply was modified 4 years, 2 months ago by
missveronica.
Yes, I am using a unique mobile number to validate the field.
As far as I remember this was explained in the README
Mobile Number Validation
Custom UM Field Validation: unique_mobile_number
If mobile number contains blank/spaces and some of these characters + ( ) – . the stored meta value must have an exact match for identification. All other characters must be digits and no other alphabetic characters are allowed in the mobile number.
Thanks again for your attention.
Thanks for letting us know.
Regards,
-
This reply was modified 4 years, 2 months ago by
The topic ‘Change username using costume field’ is closed to new replies.