Skip to:
Content
Pages
Categories
Search
Top
Bottom

Help filtering my custom activity pages with parse_args


  • Andrew
    Participant

    @snd26

    I’m filtering my activity pages using the bp_after_has_activities_parse_args filter and using conditionals depending on the activity page. But my conditionals are not working correctly for my custom pages. I’m not great with PHP but I’ve followed the instructions the best I can to put this together and I hope someone can help me with this to find a work around.

    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 each of these 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, I can use conditionals to filter some of the activity pages, but my conditionals are not working correctly for my custom pages – they work to begin with but when I click the load more button, the new items are not being filtered. 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 fine:
    
    	// 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'] = 10;
        }
    
    	
    	
    	// The following conditionals to filter my custom pages are not working when the load more button is clicked :
    	
    	// custom activity page 1
    	if (is_page( 'custom-page-1' ) ) {
    		$retval['per_page'] = 2;
    	}
    	
    	// custom activity page 2 
    	if (is_page( 'custom-page-2' ) ) {
    		$retval['per_page'] = 8;
        }
    	
    	
    	
        return $retval;
    }
    add_filter( 'bp_after_has_activities_parse_args', 'cm_activity_filter' );

    So the is_page() conditionals are not working when the load more button is clicked. Is there a way to get this to work correctly. Any help appreciated.

Viewing 4 replies - 1 through 4 (of 4 total)

  • Henry Wright
    Moderator

    @henrywright

    is_page() has been problematic for me in the past (when using with BuddyPress) but I thought those problems had been resolved. Feel free to open a Trac ticket for this.


    Andrew
    Participant

    @snd26

    Thanks Henry. I’ve got more info on this to help the core team fix this issue.

    I think all WordPress conditionals are not working correctly inside the bp_after_has_activities_parse_args filter. Only BuddyPress conditionals are working inside it.

    I also have custom member pages too using bp_after_has_members_parse_args, but WordPress conditionals inside that are all working fine, example:

    // filter member pages
    function cm_members_filter( $retval ) {
    
    	if ( is_page('custom-page-3') ) {
    		$retval['per_page'] = 1;
    	}	
    	
    	if ( is_page('custom-page-4') ) {
    		$retval['per_page'] = 5;
    	}	
    	
    	if ( is_search() ) {  
    		$retval['per_page'] = 3;
    	}	
    		
        return $retval;
    }
    add_filter( 'bp_after_has_members_parse_args', 'cm_members_filter' );

    It is just the activity parse args that is having problems with WordPress conditionals. I can provide further info to help figure out why WordPress conditionals don’t work correctly inside bp_after_has_activities_parse_args, it has something to do with the way ajax is used on the load more button to load more items:

    If I remove activity ajax features by removing <div class=”activity”></div> or by removing bp-templates/bp-legacy/js/buddypress.min.js. The load more button now works like a pagination next page button (similar to the members directory pagination). This makes the WordPress conditionals work fine and all my custom activity pages are now filtering correctly.

    So to conclude, WordPress conditionals are working OK inside bp_after_has_members_parse_args, but they are not working correctly inside bp_after_has_activities_parse_args unless the activity ajax is removed.


    Andrew
    Participant

    @snd26

    Resolved. Turns out WordPress page conditionals are not true during ajax requests. You can create conditionals using wp_get_referer() as a workaround. I created my own conditional, if anyone wants to create a page conditional to filter a custom activity loop with the parse args filter, here is a good starting point:

    //for page 'test-1'
    unction 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;
    }

    mecanographik
    Participant

    @mecanographik

    Hi @snd26,
    Thanks for sharing.

    Here is more universal version of your function i use now on a project in a addition with a group meta filter during bp ajax groups loop :

    function custom_is_page($slug) {
    
        $statement = false;
        if ( is_page( $slug ) ) {
            $statement = 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 && $slug === $referer_page->post_name ) {
                $statement = true;
            }
        }
        return $statement;
    }

    I use it in this function :

    add_action('bp_before_groups_loop', 'custom_groups_loop_meta_filter');
    function custom_groups_loop_meta_filter($meta_filter) {
    	if(custom_is_page('groups-movies')) {
    		$meta_filter = new BP_Groups_Meta_Filter( 'group_type', 'movies' );
      		return $meta_filter;
    	}
    }
    
Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar