• Creator
  • #19747
    Resolved Bill Phelps


    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?


    Bill Phelps

Viewing 4 replies - 1 through 4 (of 4 total)
  • Author
  • #19752
    Long Nguyen


    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.

    Bill Phelps

    Thanks - 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 🙂

    Bill Phelps

    I'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 🙂

    Bill Phelps

    OK. 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 in wp_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 and rwmb_user_email_html to populate fields 'user_login' and 'user_email' based on $user_id. (For some reason, the corresponding rwmb_{$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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.