BuddyDev

Search

[Resolved] Using different default Avatar for BuddyPress code


  • Participant
    Level: Guru
    Posts: 666
    calu on #27389

    Hi Brajesh, I have been using the code below to use different avatars based on gender, but it is not working anymore with typing in field gender names.

    Therefore I was looking at your suggestion for ‘using id for performance reasons’ I know the gender field id, but this is the same for both genders, so how do I find the specific id for each gender?

    Regards
    Carsten

    //Using different default Avatar for BuddyPress 
    
    //Disable Gravatar 
    
    add_filter( 'bp_core_fetch_avatar_no_grav', '__return_true' );
    
    //Using different default avatar based on BuddyPress profile field
    
    function buddydev_set_default_avatar_based_on_profile_field_data( $avatar, $params ) {
    
    	$user_id          = $params['item_id'];
    	$field_id_or_name = 32;//Gender field Id, You may use field name e.g 'Gender';
    	//I suggest using id for performance reasons
    
    	$gender = xprofile_get_field_data( $field_id_or_name, $user_id );
    	$gender = strtolower( $gender );
    	if ( $gender == 'man' ) {
    		$avatar = 'https://domain/wp-content/uploads/2020/01/avatar_male_02.jpg';
    	} elseif ( $gender == 'woman' ) {
    		$avatar = 'https:/domain/wp-content/uploads/2020/01/avatar_female_02.jpg';
    	} else {
    		$avatar = 'https://domain/wp-content/uploads/2019/10/me-sep.png';
    	}
    
    	// note, you may play with the $param['type'] and use different image for full|thumb.
    	return $avatar;
    }
    
    add_filter( 'bp_core_default_avatar_user', 'buddydev_set_default_avatar_based_on_profile_field_data', 10, 2 );
    

  • Keymaster
    (BuddyDev Team)
    Posts: 15482
    Brajesh Singh on #27400

    Hi Carsten,
    Thank you for the question.

    The profile field is fine(there should be only one).

    Please make sure that you replace the comparison

    
    $gender == 'man'
    

    and

    
    $gender == 'woman'
    
    

    with lowercase values in your locale. I am mentioning this since I know that your site is non English and the values may be different.

    Regards
    Brajesh


  • Participant
    Level: Guru
    Posts: 666
    calu on #27410

    Hi Brajesh, thanks for the clarification, it’s working now!

    Regards
    Carsten


  • Keymaster
    (BuddyDev Team)
    Posts: 15482
    Brajesh Singh on #27422

    You are welcome 🙂

The topic ‘ [Resolved] Using different default Avatar for BuddyPress code’ is closed to new replies.

This topic is: resolved
Subscribe