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,
hi @champsupertramp
Thank you for your reply.
I will test it now.
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
@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 );
}
}
@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.
-
This reply was modified 4 years, 2 months ago by
missveronica.
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
@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.
Hi @missveronicatv
Thank you for your effort
Hope you can achieve it
@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}" );
}
}
}
}
}
Hi @missveronicatv
Still not working either.
Can you see this > http://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
Hi @missveronicatv
Revision / additional information:
Currently :
site.com/user/12 -> (user id)
Targets:
site.com/regno/a123 -> (username)
Thank you
@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}" );
}
}
}
}
}