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' );
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
foreach ( $user->roles as $role )
if ( $role != 'whatever role you want to avoid' && $role != 'another role you want to avoid' )
echo $role;
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.
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' );
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!
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!
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;
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
🙂