Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display Role as part of Profile


  • dlongm01
    Participant

    @dlongm01

    Hi

    I’ve scoured the forums looking for a straightforward solution to displaying the user role as part of the BuddyPress profile. There’s a few posts but they are incomplete and now rather old.

    Does anyone have a solution? An ideal layout might be to display the role alongside the name or username.

    Any help appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)

  • shanebp
    Moderator

    @shanebp

    https://codex.wordpress.org/Function_Reference/get_userdata

    Example:

    function dlongm_show_role() {
        $user = get_userdata( bp_displayed_user_id() );
    
        if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
            foreach ( $user->roles as $role )
        	    echo $role;
        }
    }
    add_action( 'bp_before_member_header_meta', 'dlongm_show_role' );

    dlongm01
    Participant

    @dlongm01

    Excellent Shane. That looks good.

    I don’t want the bbpress role to show though, just the main user role. Can the bbp role be squelched?

    Thanks


    shanebp
    Moderator

    @shanebp

    foreach ( $user->roles as $role )
        if ( $role != 'whatever role you want to avoid' && $role != 'another role you want to avoid' )
           echo $role;

    dlongm01
    Participant

    @dlongm01

    Thanks Shane

    Sorry – I am a real novice here ask you to indulge me.

    I have tried this inside the code you already gave me but it does not work. I guess I don’t know the correct syntax for the If statement, or I do not know how to name the role. E.g. I want to exclude bbp_spectator so tried this:

    function dlongm_show_role() {
        $user = get_userdata( bp_displayed_user_id() );
    
        if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
            foreach ( $user->roles as $role )
                if ( $role != bbp_spectator) {
        	       echo "$role;
           }
        }
    }
    add_action( 'bp_before_member_header_meta', 'dlongm_show_role' );

    I guess what puzzles me is that the array has clearly got something like this in it:

    adminstratorbbp_spectator

    All I need is the first element of this string or object. I am just not clear about PHP syntax to know how to get it ….

    I appreciate your advice.


    shanebp
    Moderator

    @shanebp

    function dlongm_show_role() {
        $user = get_userdata( bp_displayed_user_id() );
        echo $user->roles[0];
    }
    add_action( 'bp_before_member_header_meta', 'dlongm_show_role' );

    dlongm01
    Participant

    @dlongm01

    Thank you ShaneBP. That’s marvellous. Does exactly what I want. Your help is very gratefully acknowledged.

    Here’s my final code. The echo line formats and capitalises the role name:

    function dlongm_show_role() {
        $user = get_userdata( bp_displayed_user_id() );
            $userole = $user->roles[0];
                echo "<span style='color:#9e152c;font-size:14pt;'>".ucwords($userole)."</span>";
           
    }
    add_action( 'bp_before_member_header_meta', 'dlongm_show_role' );

    I learned a little more about PHP too which is a bonus. I always twitch when I add PHP to my functions file because even trivial simple syntax errors can crash a site (no semi-colin, missin bracket etc.) Luckily I have a dev site and FTP access so I can get at the functions file if things go wrong – which happend several times!


    dlongm01
    Participant

    @dlongm01

    Hi Shane – one last question.

    I just realised that function gets the role ID not the role name.

    So I get the role ID ‘senior-fellow’ rather than the role name ‘MirandaNet Senior Fellow’

    I looked at the codex (various searches on ‘display role name’ or ‘get role name’) but can’t quite figure out what it’s telling me.

    This is not a crucial issue, but nice if it is simple to display the name rather than the ID.

    Thanks

    PS I realise that my original question referred to roles rather than names. I’ve only just noticed the important difference!


    shanebp
    Moderator

    @shanebp

    The role ID is the role name.

    If you know the possible role names, you can do something like:

    $userole = $user->roles[0];
    if ( $userole == 'senior-fellow' );
       echo 'Senior Fellow'; 
    elseif ( $userole == 'junior-fellow' );
       echo 'Junior Fellow'; 
    else
       echo $userole; 

    dlongm01
    Participant

    @dlongm01

    OK – thanks – that seems simple enough. I just thought that the role name might be part of the user data.

    I can manage that though.

    Cheers

    🙂

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Display Role as part of Profile’ is closed to new replies.
Skip to toolbar