Skip to:
Content

Opened 2 years ago

Closed 2 years ago

Last modified 19 months ago

#6801 closed defect (bug) (invalid)

Filtering custom activity pages with parse_args

Reported by: snd26 Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.4.0
Component: Core Keywords:
Cc:

Description

There is a bug when using WordPress page conditionals such as is_page('custom-page') inside the bp_after_has_activities_parse_args filter to filter activities. They start filtering OK, but when the load more button is clicked, the next items are not getting filtered. I've done some tests and it seems to be because of the way bp-templates/bp-legacy/js/buddypress.js uses ajax to load more activities.

Here is what I've done.

I'm creating custom activity pages by setting them up in admin>pages>add page 'custom-page-1', 'custom-page-2' etc. I've setup custom activity pages similar to the activity/index.php to create the activity loop:

<div id="buddypress"> 
     <div class="activity"> 
          <?php bp_get_template_part( 'activity/activity-loop' ); ?> 
     </div> 
</div>

Now with the parse_args filter, here is an example to show you what is working and what is not (using the per_page filter for demonstration):

function cm_activity_filter( $retval ) {

	// The following conditionals to filter are working OK:

	// Activity directory
	if ( bp_is_activity_directory() ) {
	     $retval['per_page'] = 2;
        }
	
	// User profile just-me component
	if ( bp_is_current_action( 'just-me' )) {
	     $retval['per_page'] = 3;
        }
	 
	// A custom component in the members profile I setup using bp_core_new_nav_item
	 if ( bp_is_current_action( 'custom-compenent' )) {
	      $retval['per_page'] = 4;
        }

	
	
	/* Bug: The following conditionals start off filtering OK, but 
        when the load more button is clicked, the new items are not filtering: */
	
	// custom activity page 1
	if ( is_page( 'custom-page-1' ) ) {
	     $retval['per_page'] = 5;
	}
	
 	/* search page
         * 
	 * I also have a search page for the activities & members - I use the WorPress 
	 * search box and setup the activity loop & member loop in 'search.php'.
	 */
	if ( is_search() ) {
	     $retval['per_page'] = 2;
        }
	
	
	
        return $retval;
}
add_filter( 'bp_after_has_activities_parse_args', 'cm_activity_filter' );

Note: I don't have this problem using WordPress page conditionals to filter my custom member pages with bp_after_has_members_parse_args filter, it is just when filtering my custom activity pages.

Change History (5)

#1 @snd26
2 years ago

  • Version set to 2.4.0

#2 @snd26
2 years ago

To recreate this issue:

  1. create a page in admin > pages > new page, such as 'test-1'.
  1. create the file for the new page in the theme directory 'page-test-1.php'.
  1. Make this new file page-test-1.php look something like this:
<?php

get_header(); ?>

<div class="content" id="content">
        <main id="main" class="site-main">
        
                <div id="buddypress">

                        <div class="activity">
                        
                                <?php bp_get_template_part( 'activity/activity-loop' ); ?>
                                
                        </div>
                        
                </div>

        </main>
</div>

<?php get_footer(); ?>
  1. Now go to yoursite.com/test-1 and this should now have the activity loop working OK.
  1. Now to filter this activity loop, add this to your theme's functions.php or a plugin file and we'll use the per_page parameter as an example:
<?php
/** 
 * Filter activity loop
 */
function cm_activity_filter_test( $retval ) {

        if ( is_page('test-1') ) { 
             $retval['per_page'] = 1;
        } 
        
    return $retval;
}
add_filter( 'bp_after_has_activities_parse_args', 'cm_activity_filter_test' );
  1. Now visit yoursite.com/test-1 again and this page will now show 1 activity item per page due to the above filter. But when you click the load more button, it won't show the next 1 item, it will show the next 20 items (per_page default). When you click the load more button, it no longer recognizes the is_page('test-1') conditional.

Note: This conditional works fine though if you remove bp-templates/bp-legacy/js/buddypress.min.js.

#3 @boonebgorges
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

The problem is likely that is_page( 'test-1' ) is not true during AJAX requests. AJAX requests have URLs like example.com/wp-admin/admin-ajax.php, not example.com/test-1/. As such, WP doesn't do its normal bootstrap, parsing the request according to rewrite rules, running a WP_Query, etc. This is not a bug in BuddyPress, but a side effect of how WP AJAX requests work. See http://wordpress.stackexchange.com/questions/71070/conditional-ajax-inclusion for more information.

As a workaround, you can build your own conditional that looks like this. (Untested, but this should give you a starting point.)

function bp6801_is_test_1_page() {
    $is_test_1_page = false;

    if ( is_page( 'test-1' ) ) {
        $is_test_1_page = true;
    } elseif ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
        $referer_page_id = url_to_postid( wp_get_referer() );
        $referer_page = get_post( $referer_page_id );
        if ( $referer_page instanceof WP_Post && 'test-1' === $referer_page->post_name ) {
            $is_test_1_page = true;
        }
    }

    return $is_test_1_page;
}

You can use this to tell whether to modify your 'per_page' setting, whether or not you're in an AJAX request.

#4 @snd26
2 years ago

@boonebgorges Thanks, this worked. I originally asked this in the support forums and I was encouraged to create a trac ticket for it, so I assumed it was probably a bug. I created my own conditional based on this, thanks for your time and help :)

#5 @DJPaul
19 months ago

  • Component changed from API to Core
Note: See TracTickets for help on using tickets.