Hi, perhaps you omited a dot or another php element. Anyway, below snippet add same fonction to members directory by using the appropriate filter, so you haven’t to touch the template.
Add it to child theme functions.php or bp-custom.php
Show user’s role on member directory
function who_are_you_dir() {
$user = new WP_User( bp_get_member_user_id() );
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
/**
* Fetch the user's role. See https://codex.wordpress.org/Roles_and_Capabilities
* _e('role name', 'text_domain' ) allows use of translation
* else use echo "role name";
*/
// Output
if ( $role == 'administrator') {
echo "Administrator";
}
if ( $role == 'subscriber') {
echo "Subscriber";
}
if ( $role == 'contributor') {
echo "Contributor";
}
if ( $role == 'author') {
echo "Author";
}
if ( $role == 'editor') {
echo "Editor";
}
}
}
}
add_filter ( 'bp_directory_members_item', 'who_are_you_dir' );
To show user’s role(s) on his profile, you can use this:
Show user’s role on profile
function blablabla() {
$user = new WP_User( 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', 'blablabla' );
Thanks Dan, that worked, except for the last user in the list, that one the role was missing
Which role is concerned ?
Does that member exist ? Is it pending or spammed ?
Have you tried with another display filter on member’s directory ?
That member is an editor, the only one on the site.
I tried it on another site with no editors and all were displayed, however, for that site I’ve set up a customer role of “Member”, but they were all displayed as “Contributor”
custom role, not customer
Sorry, forget previous question. Use level 7 to get editor’s role. (i modified the snippet to reflect this)
If you defined member as a member type, it’s normal as it’s not a WP role or capacity.
BP use WP roles and add his own (admin and mod) for group usage only (a wp subscriber role can be a group admin in BP, but that is just a title and has nothing to do with a wp role).
Also, avoid using member as custom term. This can lead to unexpected issues with BP which use that word almost everywhere for internal purpose.
It seems to have uncovered an issue on my other site, I modified the rolename from Contributor to Member and that fixed the display, however two of my members are showing as Subscribers even though in the user list they have the role of Member. I wonder what is causing this?
No idea. Review your custom code.