Skip to content

Instantly share code, notes, and snippets.

@dcavins
Created December 14, 2017 16:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dcavins/31c2f264d0e56cd2832cb3c63a994b97 to your computer and use it in GitHub Desktop.
Save dcavins/31c2f264d0e56cd2832cb3c63a994b97 to your computer and use it in GitHub Desktop.
Add the user's roles to the classes applied to a BuddyPress avatar.
<?php
add_filter( 'bp_get_member_avatar', 'my_add_user_level_to_avatars', 10, 2 );
/**
* @param string $value Formatted HTML <img> element, or raw avatar URL based on $html arg.
* @param array $r Array of parsed arguments. See {@link bp_get_member_avatar()}.
*/
function my_add_user_level_to_avatars( $value, $r ) {
global $members_template;
// The args have already been parsed in bp_get_member_avatar. We'll use them except for the css class.
$class = array( $r['class'] );
// Get user roles.
$user_info = get_userdata( $members_template->member->id );
if ( ! empty( $user_info->roles ) ) {
$class = array_merge( $class, $user_info->roles );
}
// Convert array to space-separated string.
$class = implode( ' ', $class );
return bp_core_fetch_avatar( array( 'item_id' => $members_template->member->id, 'type' => $r['type'], 'alt' => $r['alt'], 'css_id' => $r['id'], 'class' => $class, 'width' => $r['width'], 'height' => $r['height'], 'email' => $members_template->member->user_email ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment