BuddyDev

Search

[Resolved] Display only Active Friends for Logged in user

  • Participant
    Level: Initiated
    Posts: 8
    Joseph Lawrence Walkollie on #45538

    Thanks to the buddypress active members block one can display active members even in the sidebar of his/her buddypress site, however, I would like this to be restricted only to the active friends of the logged in user.

    Is there any plugin for that, or can someone please help me with the code to do that.
    This should not affect the buddypress main members directory, and also thanks in advance for whatever assistance provided me.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45549

    Hello Joseph,

    Welcome to the BuddyDev Forums. BuddyPress does not provide and filter to modify active members query args at the time of listing active members using Widget or block. But you can use the following code to list active friends of logged-in users.

    Try shortcode: [buddydev-active-friends-list]

    
    
    add_shortcode( 'buddydev-active-friends-list', function ( $atts ) {
    
    	if ( ! is_user_logged_in() ) {
    		return '';
    	}
    
    	$atts = shortcode_atts( array( 'max' => 5 ), $atts );
    
    	$members_args = array(
    		'user_id'         => 0,
    		'type'            => 'active',
    		'max'             => absint( $atts['max'] ),
    		'populate_extras' => true,
    		'search_terms'    => false,
    	);
    
    	$friends_ids = friends_get_friend_user_ids( get_current_user_id() );
    
    	$members_args['include'] = empty( $friends_ids ) ? array( 0 ) : $friends_ids;
    
    	ob_start();
    	?>
    	<?php if ( bp_has_members( $members_args ) ) : ?>
    
    			<div class="bd-active-friends-list avatar-block">
    
    				<?php while ( bp_members() ) : bp_the_member(); ?>
    
    					<div class="item-avatar">
    						<a href="<?php bp_member_permalink(); ?>" class="bp-tooltip" data-bp-tooltip="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a>
    					</div>
    
    				<?php endwhile; ?>
    
    			</div>
    
    		<?php else: ?>
    
    			<div class="error">
    				<?php esc_html_e( 'No active friend found.', 'buddypress' ); ?>
    			</div>
    
    		<?php endif; ?>
    	<?php
    
    	return ob_get_clean();
    } );
    
    

    Please give it a try.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 8
    Joseph Lawrence Walkollie on #45552

    Hi Keymaster,
    Thanks for the helpful response, however I think the code is still one step away from the actual purpose, it displays all friends. Can you please do a little bit more of filtering to display only the active friends.

    Once again thanks a lot.

  • Participant
    Level: Initiated
    Posts: 8
    Joseph Lawrence Walkollie on #45553

    By active friends, I mean friends that are logged in at the moment.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45556

    Hello Joseph,

    Thank you for the acknowledgement. I will look for a way how we can implement this and will update you soon.

    Regards
    Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45564

    Hello Joseph,

    Please replace type ‘active’ with ‘online’ in the ‘$members_args’ variable it will list online friends.

    Also, Note that BuddyPress consider the last 15 minutes active user as online users. You can change this threshold value using the filter.

    Please check and let me know.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 8
    Joseph Lawrence Walkollie on #45567

    Hi Keymaster,
    Thanks a lot, it really works.
    Also, concerning “Note that BuddyPress consider the last 15 minutes active user as online users. You can change this threshold value using the filter”, I would like to reduce this to 5 minutes. Hope you can help me with this as well.

    Once again thank you very much.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45626

    Hello Joseph,

    Thank you for the acknowledgement. Please try the following code it will set the online interval to 5 minutes.

    
    
    add_filter( 'bp_user_query_online_interval', function ( $interval ) {
    	return 5;
    } );
    
    

    Please check.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 8
    Joseph Lawrence Walkollie on #45968

    I’m just seeing this last response. Thanks again Keymaster for all the assistance.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 2935
    Ravi on #45973

    Thank You

The topic ‘ [Resolved] Display only Active Friends for Logged in user’ is closed to new replies.

This topic is: resolved