BuddyDev

Search

Member Types Pro and use of associated avatars

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #39991

    Hi there, I’m using the associated avatars, for each of my two gender member types.

    The issue is that avatars in BP widgets like Online Members, Recent Visitors, and Recently Active Members are not changed to these avatars, and instead display an image place holder is displayed, which is a problem.

    https://imgur.com/zJ1pwJj

    Would it be possible to have the Member Types Pro associated avatars displayed in the BP widgets?

    Regards
    Carsten

  • Keymaster
    (BuddyDev Team)
    Posts: 24712
    Brajesh Singh on #39998

    Hi Carsten,
    Thank you for using the plugin.

    It seems form the screenshot that you have some kind of custom coding(or using some plugin). It may not be calling the bp_core_fetch_avatar().

    Please share the details and Ravi or I will look into it and assist you.

    Regards
    Brajesh

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #40004

    Hi Brajesh, Thanks for helping out. I have not been able to address the issue yet, but I can see now that it is not related to MTP, as the issue persist, when using the code below I used before I started using MTP.

    I have deactivated my child theme to rule out any issues there, and I have gone through my plugins, but I can’t see that any of these could affect this.

    If you are willing to take a closer look on my site, please let me know, and I will send you the credentials.

    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://wedate.dk/wp-content/uploads/2021/02/IMG_5356-e1620142850931.jpeg';
    	} elseif ( $gender == 'woman' ) {
    		$avatar = 'https://wedate.dk/wp-content/uploads/2021/02/IMG_5357.jpeg';
    	} else {
    		$avatar = 'https://wedate.dk/wp-content/uploads/woocommerce-placeholder.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: 24712
    Brajesh Singh on #40013

    Hi Carsten,
    Thank you.

    Yes, it seems something else is the issue.

    I am unable to look into the site but I can get one of my team members to look into it. Please share the credentials and @ravisharma can look into it yesterday and assist you.

    Regards
    Brajesh

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #40017
    This reply has been marked as private.
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #40021

    Hello Carsten,

    I have checked and it seems you have not uploaded both versions of avatar i.e. full and thumb for member type. I have uploaded ones and it is working fine for me. Please check.

    Take a look:

    https://tinyurl.com/yfadmqdv

    Regards
    Ravi

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #40022

    Hi Ravi, thanks for looking into this and for solving this issue for me.

    I wasn’t aware of the required thumbnail avatar, so now it is working perfectly!

    The only issues which remains, is that the BP widget avatars are not following members loop, or the gender based member filter below?

    How do I make the code work on BP widgets as well?

    Regards
    Carsten

    
    /**
     * Filters Users based on Gender. Make sure only users of opposite genders are visible.
     */
    class BuddyDev_Gender_Based_Member_Filter {
     
        /**
         * Boot
         *
         * @return BuddyDev_Gender_Based_Member_Filter
         */
        public static function boot() {
            $self = new self();
            $self->setup();
            return $self;
        }
     
        /**
         * Setup hooks.
         */
        private function setup() {
            add_filter( 'bp_after_has_members_parse_args', array( $this, 'filter_args' ) );
     
            add_action( 'bp_template_redirect', array( $this, 'check_access' ), 10, 2 );
        }
     
        /**
         * Modifies members loop args to filter out users of same gender.
         *
         * @param array $args Array of group allowed parameters.
         *
         * @return array
         */
        public function filter_args( $args ) {
     
            // do not filter if user is not logged in, or is super admin or we are inside dashboard.
            if ( ! is_user_logged_in() || is_super_admin() || ( is_admin() && ! defined( 'DOING_AJAX' ) ) ) {
                return $args;
            }
     
            $opposite_gender = $this->get_opposite_gender( bp_loggedin_user_id() );
     
            if ( $opposite_gender ) {
                $args['member_type'] = $opposite_gender;
            }
     
            return $args;
        }
     
        /**
         * Restricts access to user profile.
         *
         * Only allows profile owner, user of opposite gender or site admins to view.
         */
        public function check_access() {
     
            if ( ! is_user_logged_in() || ! bp_is_user() || bp_is_my_profile() || is_super_admin() ) {
                return;
            }
     
            $gender = $this->get_user_gender( bp_loggedin_user_id() );
            if ( ! $gender ) {
                return;
            }
     
            // do not allow access for same gender.
            if ( bp_has_member_type( bp_displayed_user_id(), $gender ) ) {
                bp_core_add_message( __( 'Access denied.', 'yym-helper' ) );
                bp_core_redirect( bp_loggedin_user_domain() );
            }
        }
     
        /**
         * Returns user Gender.
         *
         * @param int $user_id user id.
         *
         * @return string|null
         */
        private function get_user_gender( $user_id ) {
            
            $gender       = null;
            $member_types = bp_get_member_type( $user_id, false );
     
            if ( empty( $member_types ) ) {
                $gender = null;
            } elseif ( $this->is_user_male( $user_id ) ) {
                $gender = 'male';
            } elseif ( $this->is_user_female( $user_id ) ) {
                $gender = 'female';
            }
     
            return $gender;
        }
     
        /**
         * Finds the opposite gender for the given user.
         *
         * @param int $user_id user id.
         *
         * @return string|null
         */
        private function get_opposite_gender( $user_id ) {
     
            $user_gender = $this->get_user_gender( $user_id );
            if ( ! $user_gender ) {
                return null;
            }
     
            if ( 'male' === $user_gender ) {
                return 'female';
            } elseif ( 'female' === $user_gender ) {
                return 'male';
            }
     
            return null;
        }
     
        /***
         * Checks if user is male or not.
         *
         * @param int $user_id User id to check.
         *
         * @return bool
         */
        private function is_user_male( $user_id ) {
            return bp_has_member_type( $user_id, 'male' );
        }
     
        /***
         * Checks if user is female or not.
         *
         * @param int $user_id User id to check.
         *
         * @return bool
         */
        private function is_user_female( $user_id ) {
            return bp_has_member_type( $user_id, 'female' );
        }
    }
     
    BuddyDev_Gender_Based_Member_Filter::boot();  
  • Participant
    Level: Yogi
    Posts: 1117
    calu on #40023

    Hello Ravi, apparently there are two sets of BP Block Widgets, but only one of the sets are filtered by the Gender Based Member Filter:

    https://imgur.com/6786PAs

    What seems to be the core BP blocks are not:

    https://imgur.com/gSLVz94

    What is the difference between the two sets of BP Block Widgets, obviously there is a difference?

    Regards
    Carsten

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #40024

    Hello Carsten,

    It seems You are using BP Block Widgets which was introduced in BuddyPress 9.0.0 and We have not explored much with block API so couldn’t help you with this at the moment.

    Regards
    Ravi

  • Participant
    Level: Yogi
    Posts: 1117
    calu on #40028

    Hello Ravi, that’s fine, blocks is the only way I can add widgets now, the old widget layout has gone.

    Regards
    Carsten

You must be logged in to reply to this topic.

This topic is: not resolved