BuddyDev

Search

[Resolved] Adding a second control to Member’s activity loop

Tagged: 

  • Participant
    Level: Initiated
    Posts: 18
    Mike Witt on #42317

    In the activity loop for individual members, I’d like to add an option for ascending or descending order. I can see how to change the order by adding a filter on bp_ajax_querystring and it appears that I can override members/single/activity.php which I think is where I would add the second drop down to pick ascending or descending. But I can’t figure out how to put these things together. Any help appreciated.

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

    Hi Mike,
    Thank you for the question.

    The answer will depend on which template pack is being used by you. Are you using BP legacy or Bp Nouveau?

    Please let us know and we will assist you.

    Regards
    Brajesh

  • Participant
    Level: Initiated
    Posts: 18
    Mike Witt on #42343

    Hi Brajesh, the site in question uses Legacy. Thanks.

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #42351

    Hello Mike,

    Thank you for sharing the details. With Legacy you can add the custom filter options like the following code:

    
    add_filter( 'bp_get_activity_show_filters_options', function ( $options ) {
    // If not on single user screen return default options.
    	if ( ! bp_is_user() ) {
    		return $options;
    	}
    
    	if ( ! isset( $options['asc'] ) ) {
    		$options['asc'] = __( 'Ascending Order' );
    	}
    
    	if ( ! isset( $options['desc'] ) ) {
    		$options['desc'] = __( 'Descending Order' );
    	}
    
    	return $options;
    } );
    
    

    Then you can filter activity based on these options. Please let me know if it helps or not.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 18
    Mike Witt on #42358

    Hi Ravi,

    I would actually like to create a 2nd drop-down, as I mentioned above. However, let me try to understand what you are suggesting. I think my problem is that I don’t know how to communicate the asc and desc options to bp_ajax_querystring. When I do what you are suggesting, the “asc” or “desc” parameters show up as “type” not as “sort” when they get to bp_ajax_querystring.

    I don’t know if I’m explaining that very well 🙂

    Mike

    
    function add_order_options( $options ) {
        // If not on single user screen return default options.
        if ( ! bp_is_user() ) {
            return $options;
        }
    
        $log_me = "\nVer 8:";
        $log_me .= "\nBefore: " . json_encode($options);
    
        if ( ! isset( $options['asc'] ) ) {
            $options['asc'] = __( 'Ascending Order' );
        }
    
        if ( ! isset( $options['desc'] ) ) {
            $options['desc'] = __( 'Descending Order' );
        }
    
        $log_me .= "\nAfter: " . json_encode($options);
        error_log($log_me);
        return $options;
    }
    add_filter( 'bp_get_activity_show_filters_options', 'add_order_options');
    
    function filter_bp_aqs( $bp_aqs, $object )
    { 
        /*
        $query_sort = $_GET['sort'];
    
        if ( isset( $query_sort ) && $query_sort === 'asc' )
        $bp_aqs .= 'sort=ASC';
         */
    
        error_log('filter_bp_aqs: ' . $bp_aqs); 
        return $bp_aqs;
    }
    add_filter( 'bp_ajax_querystring', 'filter_bp_aqs', 999, 2 );
    
  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #42373

    Hello Mike,

    Thank you for sharing the details. I will look for the solution and will get back to you.

    Regards
    Ravi

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #42395

    Hello Mike,

    Try the following code:

    
    
    add_action( 'bp_before_member_activity_post_form', function () {
    
    	if ( ! bp_is_user() || ! bp_is_user_activity() ) {
    		return;
    	}
    
    	?>
    
    		<label class="activity-filter-order">
    			<?php _e( 'Order' ) ?>
    			<select id="activity-filter-order">
    				<option value="DESC"><?php _e( 'Descending' ) ?></option>
    				<option value="ASC"><?php _e( 'Ascending' ) ?></option>
    			</select>
    		</label>
    	<?php
    } );
    
    add_filter( 'bp_ajax_querystring', function ( $query_string ) {
    
    	if ( ! bp_is_user() || ! bp_is_user_activity() ) {
    		return $query_string;
    	}
    
    	$sort = empty( $_POST['sort'] ) ? '' : wp_unslash( $_POST['sort'] );
    
    	if ( in_array( $sort, array( 'ASC', 'DESC' ) ) ) {
    		$query_string .= empty( $query_string ) ? "sort={$sort}" : "&sort={$sort}";
    	}
    
    	return $query_string;
    }, 99 );
    
    add_action( 'bp_enqueue_scripts', function () {
    
    	if ( ! bp_is_user() || ! bp_is_user_activity() ) {
    		return;
    	}
    
    	$data = "
    	jQuery( document ).ajaxSend(function( event, jqxhr, settings ) {
            if ( settings.data && settings.data.indexOf('action=activity_widget_filter') !== -1 ) {
    			settings.data += '&sort=' + jQuery('select#activity-filter-order').val();
    	    }
    	});
    	
    	jQuery(document).ready(function(){
    	    jQuery('#activity-filter-order').change(function() {
    		    jQuery('#activity-filter-by').trigger('change');
    	    })
    	})
    	";
    
    	wp_add_inline_script( 'bp-legacy-js', $data, 'before' );
    }, 11 );
    
    

    Please let me know if helps or not.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 18
    Mike Witt on #42407

    Thanks very much Ravi. That definitely works on my test site. I need to spend some time understanding how the ajax stuff works exactly, but that something I need to learn.

    This have been very helpful. Once again, thank you!

    -Mike

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #42410

    Hello Mike,

    Thank you for the acknowlegment. I am glad that I could help you.

    Regards
    Ravi

The topic ‘ [Resolved] Adding a second control to Member’s activity loop’ is closed to new replies.

This topic is: resolved