Skip to:
Content
Pages
Categories
Search
Top
Bottom

Update to Get Avatar Function – Change ID based off user role/capabilities

  • @jaketblank

    Participant

    Wanted to add different color borders around admin avatars, moderators, premium members, free members, etc to allow for easy identification of community roles. Unfortunately there are no CSS selectors in the avatars natively to do this…

    As a solution I updated the function bp_get_member_avatar(), it now gives an ID to each avatar depending on the user’s role/capabilities

    	function bp_get_member_avatar( $args = '' ) {
    		global $members_template;
    
    		$fullname = !empty( $members_template->member->fullname ) ? $members_template->member->fullname : $members_template->member->display_name;
    
    		$defaults = array(
    			'type'   => 'thumb',
    			'width'  => false,
    			'height' => false,
    			'class'  => 'avatar',
    			'id'     => false,
    			'alt'    => sprintf( __( 'Profile picture of %s', 'buddypress' ), $fullname )
    		);
    		//START EDIT HERE
    		$user = get_user_by('login', $members_template->member->user_login); //RETREIVE WP USER OBJECT
    						if($user){
       							if (user_can($user,'edit_users')){ //CHECK USER CAPABILIRY
    								$defaults['id'] = 'admin_avatar'; //ASSIGN ID BASED OFF USER ROLE
    							} 
    							elseif (check_role($user->ID,'moderator')){//CHECK USER ROLE
    								$defaults['id']  = 'mod_avatar';
    							}
    							elseif (user_can($user,'premium_member')){
    								$defaults['id']  = 'prem_avatar';
    							}
    							else {
    								$defaults['id'] ='free_avatar';
    							}
    						}
    
    		$r = wp_parse_args( $args, $defaults );
    		extract( $r, EXTR_SKIP );
    
    		return apply_filters( 'bp_get_member_avatar', bp_core_fetch_avatar( array( 'item_id' => $members_template->member->id, 'type' => $type, 'alt' => $alt, 'css_id' => $id, 'class' => $class, 'width' => $width, 'height' => $height, 'email' => $members_template->member->user_email ) ) );
    	}
    

    Note: This can be made much more flexible by turning it into it’s own function, but I have no need for that right not (lots of other stuff to configure).

Viewing 7 replies - 1 through 7 (of 7 total)
  • @shanebp

    Moderator

    Why would you hack a function in a core file?

    2 filter hooks are provided.

    You could use filter hook
    apply_filters( 'bp_member_avatar', bp_get_member_avatar( $args ) );
    in function bp_member_avatar()
    and figure out the id and then pass in the ‘id’ via the $args argument for function bp_get_member_avatar.

    Or you could use the filter hook
    apply_filters( 'bp_get_member_avatar', etc...
    to change the css id.

    There is no need to hack that function.
    And it’s a bad practice.

    @jaketblank

    Participant

    Read my note at the bottom – because I have a million other things to code at the moment. I agree with you that writing a function and applying a filter is a better approach, which is why I suggested it in the OP

    @jaketblank

    Participant

    Also, I posted this as a suggestion for looking at updating the function itself to contain similar code (assigning a CSS selector to displayed avatars based off user role). I think it is a no brainer and can be easily turned on/off by a Boolean in the $args array.

    It was not posted here as a “how-to” for others

    @shanebp

    Moderator

    > I have a million other things to code at the moment

    Thanks for taking the time to post your code.

    If you’d like to suggest an enhancement, please use trac to submit a ticket.

    @jaketblank

    Participant

    Thanks, will do – sorry if I came off curt; I’m the sole developer for a massive community endeavor (tying together bigbluebutton, buddypress, simple:press, woocommerce, sensei, software licensing, groups, subscriptions, informational video portal, document library, and of course a blog) and a bit burnt out to say the least. Just did a quick google search for the BP forum and looked for the suggestion thread cause I felt this was worth a discussion

    @jaketblank

    Participant

    Oh, I’m also the only person developing the plugins for sale in the store, so I was in no means exaggerating by saying I have a million things to code – currently being pressured to start closed betas 🙁

    @shanebp

    Moderator

    No problem.
    Good luck with the project.
    Post a link when it goes public.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Update to Get Avatar Function – Change ID based off user role/capabilities’ is closed to new replies.
Skip to toolbar