BuddyDev

Search

[Resolved] Auto Friendship Pro (will one of these rules work – and implement backwards)?

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

    Hi Chris,
    No issues.

    1. No, It will not have any effect on existing users. If you need that, please use our BuddyPress Custom development services. The time taken is beyond what we offer as forum support.

    Here is the update code for excluding some domains. Please feel free to add more.

    
    
    /**
     * Add users from same domain as friend on account activation.
     *
     * @param int $user_id user id.
     */
    function buddydev_add_friends_on_activation_by_email_domain( $user_id ) {
    
    	$user = get_user_by( 'id', $user_id );
    	if ( ! $user ) {
    		return;
    	}
    
    	$email_parts = explode( '@', $user->user_email );
    
    	if ( count( $email_parts ) !== 2 ) {
    		return;
    	}
    
    	$domain = $email_parts[1];
    	// Lis of excluded domains.
    	$excluded_domain = array(
    		'gmail.com',
    		'outlook.com',
    		'yahoo.com',
    	);
    
    	// skip if in the excluded domain list.
    	if ( in_array( $domain, $excluded_domain ) ) {
    		return;
    	}
    
    	$potential_friend_ids = buddydev_get_users_with_email_domain( $domain );
    
    	if ( empty( $potential_friend_ids ) ) {
    		return;
    	}
    
    	foreach ( $potential_friend_ids as $potential_friend_id ) {
    
    		if ( $potential_friend_id == $user_id || friends_check_friendship( $user_id, $potential_friend_id ) ) {
    			continue;
    		}
    
    		friends_add_friend( $potential_friend_id, $user_id, true );
    	}
    
    }
    
    add_action( 'bp_core_activated_user', 'buddydev_add_friends_on_activation_by_email_domain' );
    
    /**
     * Get all users who have given email domain.
     *
     * @param string $domain domain name.
     *
     * @return array
     */
    function buddydev_get_users_with_email_domain( $domain ) {
    	global $wpdb;
    
    	$pattern       = '%@' . trim( $domain );
    	$sql_statement = $wpdb->prepare( "SELECT ID FROM {$wpdb->users} WHERE user_email LIKE %s", esc_sql( $pattern ) );
    
    	return $wpdb->get_col( $sql_statement );
    }
    
    

    Regards
    Brajesh

    P.S:- Please do not copy this code from the forum notification mail. Instead use it form the support page here.

  • Participant
    Level: Initiated
    Posts: 12
    Choyt on #32259

    Thanks. But it looks like this isn’t working. We have people that are getting connected because of gmail.com, for example.

  • Participant
    Level: Initiated
    Posts: 12
    Choyt on #32261

    Scratch that – I think we found the issue.
    Code seems to be working – thank you!

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

    You are welcome.
    I am glad it worked.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved