BuddyDev

Search

[Resolved] Problem to Setting Auto Friendship

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #36570

    Hi Emanuele,
    Thank you for the patience. It seems @ravisharma was not available and could not assist you today. Either He or I will be providing you the code within next 24 hours.

    Regards
    Brajesh

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2936
    Ravi on #36578

    Hello Emanuele,

    Please try the following code. It will add all other users with the same member type add as friends.

    
    /**
     * Add as friend other user with same member type on user member change.
     */
    add_action( 'bp_set_member_type', function ( $user_id, $member_type ) {
    
        $users = new BP_User_Query(
            array(
                'member_type'     => $member_type,
                'populate_extras' => false,
            )
        );
    
        if ( ! $users->total_users ) {
            return;
        }
    
        foreach ( $users->user_ids as $id ) {
    
            if ( $id == $user_id ) {
                return;
            }
    
            if ( ! friends_check_friendship( $user_id, $id ) ) {
                friends_add_friend( $id, $user_id, true );
            }
        }
    }, 10, 2 );
    
    

    Please do let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 16
    Emanuele Maria Esposito on #36686

    Hi guys,
    Does this code become friends with all the members of the same type right?
    Because I would only need it for a specific type of member called “partner”.

    thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #36688

    Hi Emanuele,
    You are right. It would do so for all member types.

    Here is the code that only targets the ‘partner’ member type

    
    
    /**
     * Add as friend other user with same member type on user member change.
     */
    add_action( 'bp_set_member_type', function ( $user_id, $member_type ) {
    
    	if ( 'partner' !== $member_type ) {
    		return;
    	}
    
    	$users = new BP_User_Query(
    		array(
    			'member_type'     => $member_type,
    			'populate_extras' => false,
    		)
    	);
    
    	if ( ! $users->total_users ) {
    		return;
    	}
    
    	foreach ( $users->user_ids as $id ) {
    
    		if ( $id == $user_id ) {
    			return;
    		}
    
    		if ( ! friends_check_friendship( $user_id, $id ) ) {
    			friends_add_friend( $id, $user_id, true );
    		}
    	}
    }, 10, 2 );
    
    

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 16
    Emanuele Maria Esposito on #36705

    Hi Brajesh,
    I tried the code but it doesn’t seem to work.

    How should I best set the plugin?
    This is currently the case
    https://snipboard.io/JPKx4l.jpg

    Thanks

  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #36713

    Hi Emanuele,
    Thank you for the reply.

    1. Please use the updated code. There was a bug in the previous snippet.

    
    
    /**
     * Add as friend other user with same member type on user member change.
     */
    add_action( 'bp_set_member_type', function ( $user_id, $member_type ) {
    
    	if ( 'partner' !== $member_type ) {
    		return;
    	}
    
    	$users = new BP_User_Query(
    		array(
    			'member_type'     => $member_type,
    			'populate_extras' => false,
    		)
    	);
    
    	if ( ! $users->total_users ) {
    		return;
    	}
    
    	foreach ( $users->user_ids as $id ) {
    
    		if ( $id == $user_id ) {
    			continue;
    		}
    
    		if ( ! friends_check_friendship( $user_id, $id ) ) {
    			friends_add_friend( $id, $user_id, true );
    		}
    	}
    }, 10, 2 );
    
    

    2. The above code is independent of the Friendship Pro plugin. It will work with any rules.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 16
    Emanuele Maria Esposito on #36727

    Hi Brajesh,
    unfortunately, it still doesn’t work.
    As you can see in the image the friend’s list remains at 0.
    https://snipboard.io/AKMzyu.jpg

    I also tried to manually re-enter users in the plugin but it doesn’t work.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2936
    Ravi on #36732

    Hello Emanuele,

    Please provide temporary access to your admin as well guest access of your staging site so that I can check what is going on there.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 16
    Emanuele Maria Esposito on #36736
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24237
    Brajesh Singh on #36737

    Hi Emanuele,
    Did you try to change a user’s member type after puttin the code from my last update?

    Regards
    Brajesh

The topic ‘ [Resolved] Problem to Setting Auto Friendship’ is closed to new replies.

This topic is: resolved