BuddyDev

Search

[Resolved] Prevent buddypress unfollow function for specified users

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #14508

    Hello Brajesh,

    I use this code stated below to force users to follow a set of users upon registration. How can I prevent users from unfollowing this users with the stated IDs
    (Users should not be able to unfollow the stated users if possible to hide the unfollow button for the stated user IDs)

     function auto_follow_users( $user_id ){
    	if ( function_exists( 'bp_follow_start_following' ) ){
    
    	// Add your user ids here
    	$users_to_follow = array( '606', '617', '619', '608', '548' );
    
    		foreach ( $users_to_follow as $follow_user ) {
    			bp_follow_start_following( array( 'leader_id' => $follow_user, 'follower_id' => $user_id ) );			
    		}
    	}
    }
    add_action('bp_core_activated_user', 'auto_follow_users', 10, 1); 

    I would like to hide the unfollow button across the entire site for all this users with IDs ( ‘606’, ‘617’, ‘619’, ‘608’, ‘548’ )

    Thank you

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #14535

    Please use the following code to hide the button

    
    
    /**
     * @param array $args button args.
     * @param int   $leader_id leader to be followed.
     * @param int   $follower_id follower id.
     *
     * @return mixed
     */
    function buddydev_hide_unfollow_button_for_users( $args, $leader_id, $follower_id ) {
    
    	if ( 'following' !== $args['id'] ) {
    		return $args;
    	}
    
    	// user ids who can not be unfollowed.
    	$special_user_ids = array( 1,2,3,67 );
    
    	if ( in_array( $leader_id, $special_user_ids ) ) {
    		$args['id'] = '';
    	}
    
    	return $args;
    
    }
    add_filter( 'bp_follow_get_add_follow_button', 'buddydev_hide_unfollow_button_for_users', 10, 3 );
    

    It only hides the button but does not do anything else to stop unfollowing.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #14549

    The code worked successfully thank you very much Brajesh, God bless.

  • Keymaster
    (BuddyDev Team)
    Posts: 24706
    Brajesh Singh on #14552

    Thank you for confirming. I am glad it worked 🙂

The topic ‘ [Resolved] Prevent buddypress unfollow function for specified users’ is closed to new replies.

This topic is: resolved