• Resolved edharris

    (@edharris)


    I’m working on adding the member’s user role in the header area of the Profile. I’ve pasted the code snippet I started from below, but this just returns the user role of the currently logged in user, so in my case it shows “Administrator” regardless of the member profile I’m looking at.
    Can anyone suggest how to modify this so it returns the user role name of the profile displayed?

    add_action( 'um_after_header_meta', 'add_user_role', 10 );
    function add_user_role() {
    global $current_user;
    $role = $current_user->roles[0];
    $role_name = $role ? wp_roles()->get_names()[ $role ] : '';
    echo '<div class="profile-role">',$role_name,'</div>';
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • @edharris

    Yes, $current_user is the visitor,
    um_profile_id() is the visited page’s user ID.

    https://docs.ultimatemember.com/article/124-umprofileid

    If you are using multiple Roles per User,
    it’s best to use the Role with the highest priority.

    Using the function name add_user_role might give conflicts,
    it’s better to use a custom name.

    add_action( 'um_after_header_meta', 'my_9753_add_user_role_meta', 10 );
    function my_9753_add_user_role_meta() {
    
        $role = UM()->roles()->get_priority_user_role( um_profile_id() );
        $role_name = $role ? wp_roles()->get_names()[ $role ] : '';
        echo '<div class="profile-role">' . $role_name . '</div>';
    }
    • This reply was modified 6 months, 4 weeks ago by missveronica.
    Thread Starter edharris

    (@edharris)

    Works perfectly – thanks @missveronicatv !

    On your point about a user potentially having multiple roles, does UM allow you to assign multiple roles to a user as part of the plugin? Or is that only possible by combining with another plugin like User Roles Editor, etc?

    @edharris

    Yes, you can use the “User Role Editor” plugin or a code snippet like:

    https://www.ads-software.com/plugins/user-role-editor/

    $user = new WP_User( $user_id );
    
    $user->add_role( 'retailer' );
    $user->add_role( 'customer' );

    https://docs.ultimatemember.com/article/1494-how-to-set-role-priority-for-user-roles

    Users may have multiple roles. In this case, the Ultimate Member plugin uses settings from the role with the highest priority.

    Thread Starter edharris

    (@edharris)

    Thanks! Last question on this topic: If I want to display that similar User Role badge on each card on a Member Directory grid, it looks like I need to use something different to um_profile_id().

    I tried this code, but am back to it display my logged-in user role (Administrator) on every card. Any tips for this situation?

    add_action( 'um_members_just_after_name', 'member_card_user_role_badge', 10 );
    function member_card_user_role_badge() {
    $role = UM()->roles()->get_priority_user_role( um_profile_id() );
    $role_name = $role ? wp_roles()->get_names()[ $role ] : '';
    echo '<div class="member-card-role">' . $role_name . '</div>';
    }

    @edharris

    You can try this code snippet for both your cases

    add_action( 'um_after_header_meta', 'my_add_user_role_meta', 10, 1 );
    function my_add_user_role_meta( $user_id ) {
    
        $role = UM()->roles()->get_priority_user_role( $user_id );
        $role_name = $role ? wp_roles()->get_names()[ $role ] : '';
        echo '<div class="profile-role">' . $role_name . '</div>';
    }
    Thread Starter edharris

    (@edharris)

    Perfect! Thanks again

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