#6801 closed defect (bug) (no action required)
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)
#3
@
9 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.
To recreate this issue:
page-test-1.php
look something like this:is_page('test-1')
conditional.Note: This conditional works fine though if you remove
bp-templates/bp-legacy/js/buddypress.min.js
.