• Resolved andreasml

    (@andreasml)


    Hi

    I would like to add a field in users’ profile form, displaying the user’s current role. I would like this to be entered automatically by default (based on users roles), not needing to choose the role manually. Any ideas what would this field be?

    Regards

    Andreas

    • This topic was modified 4 years ago by andreasml.
    • This topic was modified 4 years ago by andreasml.
Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @andreasml

    Have you tried using the User Meta settings in the Profile form? You can use it to display the User’s role in the Meta section of the profile. https://drive.google.com/file/d/1MQsUlfddeia4LHjgS6mPOOWdt1E1uV8g/view?usp=sharing

    Go to WP Admin > Ultimate Member > Forms > Edit a Profile Form > see “User Meta” section. You will be able to select the “Role” field there to display.

    Regards,

    Thread Starter andreasml

    (@andreasml)

    Hi @champsupertramp

    Thank you for your reply. It does help! However, it displays only the role of member and not the other roles of the user. For example the user may have two or three roles, i.e. member or a custom-made role, but it is displayed only the member role.
    Any ideas to fix it?

    Regards,

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @andreasml

    You can try this code snippet to display multiple roles in the user meta section:

    add_filter("um_show_meta_item_html", function( $html, $key ){
    
        if( in_array( $key, ['role_radio','role_select'] ) ){
            // Get the user object.
            $user_id = um_user("ID");
            $roles = array();
            $user = new WP_User( $user_id );
            $arr_role_names = [];
            foreach( $user->roles as $role ){
                $arr_role_names[ ] = $role ? wp_roles()->get_names()[ $role ] : '';
            }
    
            return implode(", ", $arr_role_names );
        }
    
        return $html;
    }, 10, 2);

    Regards,

    Thread Starter andreasml

    (@andreasml)

    Thanks

    Where should I add this code?

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @andreasml

    You can add it to your theme’s functions.php file or use Code Snippets plugin.

    Regards,

    Thread Starter andreasml

    (@andreasml)

    Thread Starter andreasml

    (@andreasml)

    Hi @champsupertramp

    I just noticed that your code works if more than one roles exist on the same user. In this case, both roles are displayed in the user meta section. On the contrary, when the user has just one role nothing is displayed. I assume that the code needs some modification in order to cover these cases as well. Could you please provide this?

    Regards,

    Andreas

    Thread Starter andreasml

    (@andreasml)

    @champsupertramp

    Update to the previous message:

    When the user role is one of the default roles (Member, Subscriber or Admin), it is displayed even if it is the sole role of the user.
    If the user role is a custom-made role (eg Author, Editor or Administrator), it is only displayed if the member has also one of the default roles (Member, Subscriber, or Admin), otherwise the user meta is left blank.

    Regards

    • This reply was modified 4 years ago by andreasml.
    • This reply was modified 4 years ago by andreasml.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @andreasml

    You can try this code snippet. I’ve updated it:

    add_filter("um_show_meta_item_html", function( $html, $key ){
    
        if( in_array( $key, ['role_radio','role_select'] ) ){
            // Get the user object.
            $user_id = um_user("ID");
            $roles = array();
            $user = new WP_User( $user_id );
            $arr_role_names = [];
            if( count(  $user->roles ) > 1 ){ 
               foreach( $user->roles as $role ){
                $arr_role_names[ ] = $role ? wp_roles()->get_names()[ $role ] : '';
               }
            }else{
                $role = $user->roles[0];
                $arr_role_names[ ] = $role ? wp_roles()->get_names()[ $role ] : '';
            }
    
            return implode(", ", $arr_role_names );
        }
    
        return $html;
    }, 10, 2);

    Regards,

    Thread Starter andreasml

    (@andreasml)

    Ηι @champsupertramp

    Many thanks for your efforts. Unfortunately, it stills does not work. It only displays the roles when one of the role is either member, or subscriber or Admin.

    Pity

    Regards

    Andreas

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @andreasml

    Could you please try this one?

    add_filter("um_show_meta_item_html", function( $html, $key ){
    
        if( in_array( $key, ['role_radio','role_select'] ) ){
            // Get the user object.
            $user_id = um_user("ID");
            $roles = array();
            $user = new WP_User( $user_id );
            $arr_role_names = [];
            if( count(  $user->roles ) > 1 ){ 
               foreach( $user->roles as $role ){
                $arr_role_names[ ] = $role ? wp_roles()->get_names()[ $role ] : '';
               }
            }else{
                $role = um_user("role");
                $arr_role_names[ ] = $role ? wp_roles()->get_names()[ $role ] : '';
            }
    
            return implode(", ", $arr_role_names );
        }
    
        return $html;
    }, 10, 2);

    Regards,

    Thread Starter andreasml

    (@andreasml)

    Hi @champsupertramp,

    Unfortunately, it does not work either

    Regards

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @andreasml

    Sorry for the late response. Could you please tell us if you’re still having issues?

    Feel free to re-open this thread by changing the topic status to “Not Resolved” so that we can get back to you.

    Regards,

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Display user role in profile’ is closed to new replies.