Change User Role
Support › MB User Profile › Change User RoleResolved
- This topic has 4 replies, 2 voices, and was last updated 4 years, 11 months ago by
Bill Phelps.
-
AuthorPosts
-
May 22, 2020 at 10:51 PM #19747
Bill Phelps
ParticipantHi
I'd like admins to be able to edit the Role of a specified user on the Profile page.
Is it possible to replicate the function of this default dropdown from the "Edit User" page on the front end?
Thanks!
Bill Phelps
May 23, 2020 at 9:20 AM #19752Long Nguyen
ModeratorHi,
The shortcode supports an attribute
user_id
to update the specific user ID, you can pass the ID to edit the user profile in the frontend.[mb_user_profile_info id="user_profile_role" user_id=5]
You can also update the user role in the frontend with a custom field, please follow my sample code and this screen record
function register_user_profile_fields($meta_boxes) { $prefix = 'user_profile_'; $meta_boxes[] = [ 'id' => $prefix . 'role', 'title' => 'Update user role', 'type' => 'user', 'fields' => [ [ 'name' => 'Custom user role', 'id' => 'role', // Must use 'role' 'type' => 'select', 'options' => array( 'administrator' => 'Administrator', 'editor' => 'Editor', 'contributor' => 'Contributor' ) ], ], ]; return $meta_boxes; } add_action( 'rwmb_meta_boxes', 'register_user_profile_fields',2, 1); add_action( 'rwmb_profile_after_process', function( $config, $user_id ) { $role = rwmb_meta( 'role', array( 'object_type' => 'user' ), $user_id ); wp_update_user( array( 'ID' => $user_id, 'role' => $role ) ); }, 99, 2 );
For more information about user role, please follow the documentation.
https://wordpress.org/support/article/roles-and-capabilities/#rolesMay 23, 2020 at 4:27 PM #19760Bill Phelps
ParticipantThanks - that's a very speedy and useful reply!
I think I've got enough here to make progress. Rather than manually adjusting user_id, I'll try including $user_id within do_shortcode()...
$form = "[mb_user_profile_info id='user_profile_role' user_id={$user_id}]";
echo do_shortcode( $form );
I'll let you know how I get on 🙂
May 24, 2020 at 4:39 AM #19765Bill Phelps
ParticipantI've now hit a problem with 'user_login' and 'user_email' on my front-end profile page.
All the other fields display the correct info for $user_id, but 'user_login' and 'user_email' are showing my own (ie: current_user) data.
When I update the form, I get the error message: "Sorry, that email address is already used!"
And also (bizarrely): "Your information has been successfully submitted. Thank you."All the other fields update OK, but 'user_login' and 'user_email are unchanged and 'role' just seems to affect the meta_value of 'role' in 'wp_usermeta', without touching the meta_value of 'wp_capabilities'. I'm guessing this may get resolved once I get the 'user_login' and 'user_email' problem fixed.
One other issue I have is that there are now two 'role' fields showing on the usual backend 'Edit User' page.
Any help you can give will be greatly appreciated, of course 🙂
May 25, 2020 at 7:13 PM #19800Bill Phelps
ParticipantOK. This seems to be working now. For anyone else interested...
- The 'role' select field is now created only when
is_admin() == false
- which fixes the duplication on the 'Edit User page. - WordPress doesn't know anything about this 'role' select field - at least, not in relation to a user's roles and capabilities. Hence the need to use action
rwmb_profile_after_process
to find the just-saved 'role' meta_value inwp_usermeta
and copy it across to 'wp_capabilities', which is where WordPress expects to find it.For the same reason, whenever we display the frontend form we need to ensure that the 'role' meta_value reflects the contents of 'wp_capabilities' - in case the user's role has been updated via the 'Edit User' page which doesn't update the 'role' meta_value.
- I'm using filters
rwmb_user_login_html
andrwmb_user_email_html
to populate fields 'user_login' and 'user_email' based on$user_id
. (For some reason, the correspondingrwmb_{$field_id}_field_meta
filters don't seem to work for this.) I've also specified 'user_login' as'disabled' => true, 'readonly' => true, 'save_field' => false
- because it's not supposed to be changed.
Hope this helps someone.
- The 'role' select field is now created only when
-
AuthorPosts
- You must be logged in to reply to this topic.