BuddyDev

Search

[Resolved] Who’s Online widget adjustment

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #44998

    Hello great team 🙂

    I have been trying to make some small adjustments in the Who’s Online widget along with your great plugin https://buddydev.com/plugins/bp-featured-members/ .

    1.I would like to display the widget in the profile page using the bp_member_header_actions. So i have used the code from this page https://digwp.com/2010/04/call-widget-with-shortcode/ to call the widget with a shortcode and then the below code

    function my_widget_on_profile () {
            $user_id = get_current_user_id();	
            if ( is_user_logged_in() && bp_is_my_profile() && bp_featured_members()->is_featured( $user_id ) ) { ?>
       <div class="whos-online"><?php echo do_shortcode( '[widget widget_name="BP_Core_Whos_Online_Widget"]' ); ?></div>
       <?php } }
    
    add_action( 'bp_member_header_actions', 'my_widget_on_profile' );

    and it seems to work fine. Just to confirm, is this the only way to implement this?

    2.I would like to display results only of online users that are flagged as Featured members (and exclude the current_user), and i found Brajesh suggestion in https://buddydev.com/support/forums/topic/change-whos-online-to-friend-only/ along with an older help from Ravi from https://buddydev.com/support/forums/topic/return-in-bp_members_suggestions_query_args-only-bp-featured-members/ . So i made something like this

    function show_online_members_only_widget( $args ) {
    	
    	if ( ! function_exists( 'bp_featured_members' ) ) {
    		return $args;
    	}
    
    	$current_user_id = get_current_user_id();
    
    	if ( $current_user_id && bp_featured_members()->is_featured( $current_user_id ) ) {
    		$args['exclude'] = $current_user_id;
    	} else {
    		$args['include'] = array( 0 );
    	}
    
    	$args['meta_key'] = '_is_featured';
    
    	return $args;
    }
    add_filter( 'bp_after_has_members_parse_args', 'show_online_members_only_widget' );

    and seems to work fine(please could you also confirm), but there is one problem. bp_after_has_members_parse_args is affecting also my main /members page and i don’t want that. Is there a way to pass those $args only to the results of the Who’s Online widget?

    Thank you so much for your time.

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #45003

    Hi,
    Thank you for the questions.

    1. You are doing it correctly.

    2. Please make sure to put the following code at the beginning of your function

    
    if ( ! is_user_logged_in() || empty( $args['type'] ) || $args['type'] !='online' ) {
    		return $args;
    	}
    

    That will make sure it does not affect your main loop.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #45005

    Hello Brajesh,

    Thank you so much for your help.
    It works correct now.

    One last question.

    I need to filter the title of the widget but it seems that i am doing something wrong with my code

    add_filter( 'widget_title','enostalgia_show_online_members_only_widget_title', 10, 3 );
    function show_online_members_only_widget_title( $title, $settings, $id_base ) {
    
        if ( 'Ποιος είναι συνδεδεμένος' == $title ) 
        return '<p class="notranslate stay-in-touch" style="color: #b8860b;">WHO IS ONLINE IN OUR FORUM</p>';
    }

    because it removes titles from other widgets also.

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #45006

    Hi,
    The filter ‘widget_title’ is dependent on plugin offering widget(some plugins may not offer it).

    Depending on which widget you are filtering, the issue is either the plugin not using widget_title filter or the title being different(or the plugin offering the filter but escaping the title).

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #45011

    Hello Brajesh,

    Thank you for your response.
    I was speaking about filtering the title of the Who’s Online Buddypress widget and i’ve noticed that it’s offering a ‘widget_title’ filter. My code is filtering the title but for some reason it affects also some other widgets.

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #45021

    oops, you forgot to add a line at the bottom of that function.

    Here is it updated for you.

    
    add_filter( 'widget_title', 'enostalgia_show_online_members_only_widget_title', 10, 3 );
    function show_online_members_only_widget_title( $title, $settings, $id_base ) {
    
    	if ( 'Ποιος είναι συνδεδεμένος' == $title ) {
    		return '<p class="notranslate stay-in-touch" style="color: #b8860b;">WHO IS ONLINE IN OUR FORUM</p>';
    	}
    
    	return $title;
    }
    

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 71
    Lefteris on #45023

    OMG , thank you Brajesh :))

  • Keymaster
    (BuddyDev Team)
    Posts: 24593
    Brajesh Singh on #45034

    You are welcome 🙂

The topic ‘ [Resolved] Who’s Online widget adjustment’ is closed to new replies.

This topic is: resolved