Skip to:
Content
Pages
Categories
Search
Top
Bottom

Adding a subnav with new page content to the profile/activity page


  • godavid33
    Participant

    @godavid33

    Hello,

    I am attempting to create a unified activity loop including personal and friends activity. You would think that it would be easy to find this, but alas I am journeying into custom coding my own solution, but it seems I can’t get past the first (and likely largest) initial roadblock. I can add a tab to the profile sub navigation no problem using the follow:

    
    function my_setup_nav() {
    	global $bp;
    	
    	bp_core_new_subnav_item( array( 
    		'name' => 'My Wall', 
    		'slug' => 'wall', 
    		'parent_url' => 'http://go-david.com/fsc/members/gabriel/activity/', 
    		'parent_slug' => 'activity', 
    		'screen_function' => 'my_profile_page_function_to_show_screen', 
    		'position' => 11, 
    		'item_css_id' => 'home' 
    	) );
    	
    }
    add_action( 'bp_setup_nav', 'my_setup_nav' );
    

    and then I try to add custom content as I have seen in code snippets in other forum posts:

    
    function my_profile_page_function_to_show_screen() {
    
    	//add title and content here – last is to call the members plugin.php template
    	add_action( 'bp_template_title', 'my_profile_page_function_to_show_screen_title' );
    	add_action( 'bp_template_content', 'my_profile_page_function_to_show_screen_content' );
    	bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    }
    
    function my_profile_page_function_to_show_screen_title() {
    	echo '<h1 class="big">wall</h1>';
    }
    
    function my_profile_page_function_to_show_screen_content() {
    	echo '<h1 class="big">content</h1>';
    }
    

    But all I get, when I go to the My Wall tab is the same content as the personal tab. How can I either a.) Customize the query string bp_has_activities( bp_ajax_querystring( ‘activity’ )) in activity-loop.php or b.) Just create my own template to display here.

    The latter solution is optimal, as I know I can only pass a single scope parameter to that query string so I would rather just create my own custom loop that aggregates all of the activity.

    To clarify, I want the member’s profile activity page to be a single page with all personal, friends, and group activity (including mentions, invitations, etc.).

    Thanks in advance,
    Gabe

    **P.S. Bonus points if you explain why the activity.php template is being called**

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

  • godavid33
    Participant

    @godavid33

    I believe the above doesn’t work correctly because I am adding the new content as a subnav which by default tried to inherit the parent template (correct me if I’m wrong).

    Here is what I came up with (this all goes in functions.php), it still needs fine tuning and testing (for example I broke pagination and commenting):

    
    function my_test_setup_nav() {
            global $bp;
           
            $parent_slug = 'wall';
            $child_slug = 'all';
    //name, slug, screen, position, default subnav
            bp_core_new_nav_item( array( 'name' => __( 'test' ), 'slug' => $parent_slug, 'screen_function' => 'my_profile_page_function_to_show_screen', 'position' => 40, 'default_subnav_slug' => 'test_sub' ) );
    }
    
    function my_profile_page_function_to_show_screen() {
    	//add title and content here - last is to call the members plugin.php template
    	add_action( 'bp_template_title', 'my_profile_page_function_to_show_screen_title' );
    	add_action( 'bp_template_content', 'my_profile_page_function_to_show_screen_content' );
    	bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/plugins' ) );
    }
    function my_profile_page_function_to_show_screen_title() {
    	echo 'something';
    }
    
    function my_profile_page_function_to_show_screen_content() {
    	global $activities_template;
    
    	$personal_activites = array();
    	$friends_activites = array();
    	$group_activites = array();
    	
    	//Get friends Loop
    	do_action( 'bp_before_activity_loop' );
    	if ( bp_has_activities( bp_ajax_querystring( 'activity' )."&scope=friends") ) :
    		$friends_activites = $activities_template->activities;
    	endif;
    	do_action( 'bp_after_activity_loop' ); 
    	
    	//Get personal Loop
    	do_action( 'bp_before_activity_loop' );
    	if ( bp_has_activities( bp_ajax_querystring( 'activity' )."&scope=just-me") ) :
    		$personal_activites = $activities_template->activities; 
    	endif;
    	do_action( 'bp_after_activity_loop' ); 
    	
    	//Get groups Loop
    	do_action( 'bp_before_activity_loop' );
    	if ( bp_has_activities( bp_ajax_querystring( 'activity' )."&scope=groups") ) :
    		$group_activites = $activities_template->activities; 
    	endif;
    	do_action( 'bp_after_activity_loop' ); 
    	
    	do_action( 'bp_before_member_activity_post_form' );
    		bp_get_template_part( 'activity/post-form' );
    	do_action( 'bp_after_member_activity_post_form' );	
    	
    	$activities_template->activities = array_merge($personal_activites,$group_activites,$friends_activites);
    	quickSort( $activities_template->activities );
    
    ?>
    	<?php do_action( 'bp_before_activity_loop' ); ?>
    
    		<?php /* Show pagination if JS is not enabled, since the "Load More" link will do nothing */ ?>
    		<noscript>
    			<div class="pagination">
    				<div class="pag-count"><?php bp_activity_pagination_count(); ?></div>
    				<div class="pagination-links"><?php bp_activity_pagination_links(); ?></div>
    			</div>
    		</noscript>
    
    		<?php if ( empty( $_POST['page'] ) ) : ?>
    
    			<ul id="activity-stream" class="activity-list item-list">
    
    		<?php endif; ?>
    
    		<?php while ( bp_activities() ) : bp_the_activity(); ?>
    
    			<?php bp_get_template_part( 'activity/entry' ); ?>
    
    		<?php endwhile; ?>
    
    		<?php if ( bp_activity_has_more_items() ) : ?>
    
    			<li class="load-more">
    				<a href="#more"><?php _e( 'Load More', 'buddypress' ); ?></a>
    			</li>
    
    		<?php endif; ?>
    
    		<?php if ( empty( $_POST['page'] ) ) : ?>
    
    			</ul>
    
    		<?php endif; ?>
    
    	<?php do_action( 'bp_after_activity_loop' ); ?>
    
    	<form action="" name="activity-loop-form" id="activity-loop-form" method="post">
    
    		<?php wp_nonce_field( 'activity_filter', '_wpnonce_activity_filter' ); ?>
    
    	</form>
    <?php	
    }     
    
    add_action( 'bp_setup_nav', 'my_test_setup_nav' );
    
    // Non-recurive Quicksort for an array of Person objects
    // adapted from http://www.algorithmist.com/index.php/Quicksort_non-recursive.php
    function quickSort( &$array )
    {
    	$cur = 1;
    	$stack[1]['l'] = 0;
    	$stack[1]['r'] = count($array)-1;
    
    	do{
    		$l = $stack[$cur]['l'];
    		$r = $stack[$cur]['r'];
    		$cur--;
    
    		do{
    			$i = $l;
    			$j = $r;
    			$tmp = $array[(int)( ($l+$r)/2 )];
    
    			// partion the array in two parts.
    			// left from $tmp are with smaller values,
    			// right from $tmp are with bigger ones
    			do{
    				while( strtotime($array[$i]->date_recorded) > strtotime($tmp->date_recorded) )
    					$i++;
    
    				while( strtotime($tmp->date_recorded) > strtotime($array[$j]->date_recorded) )
    					$j--;
    
    				// swap elements from the two sides
    				if( $i <= $j){
    					$w = $array[$i];
    					$array[$i] = $array[$j];
    					$array[$j] = $w;
    
    					$i++;
    					$j--;
    				}
    
    			}while( $i <= $j );
    
    			if( $i < $r ){
    				$cur++;
    				$stack[$cur]['l'] = $i;
    				$stack[$cur]['r'] = $r;
    			}
    			$r = $j;
    
    		}while( $l < $r );
    
    	}while( $cur != 0 );
    
    }
    

    I’m sure there is a more elegant and seamless way to do it, and I will try to figure out exactly what that is. I’ll probably end up making templates for this to try and have cleaner code and stop the ajax bugs I created.

    But for now, congratulations, you now have an aggregate news feed.


    godavid33
    Participant

    @godavid33

    Wow, so I’m dumb (or at least not as good at googling as I once thought). So, the best most efficient way to do this is using buddypress query filters. No need to add a new tab. Here is the code I ended up with (by editing activity-loop.php)

    
    	$friends = friends_get_friend_user_ids( bp_loggedin_user_id() );
    	$friends[] = bp_loggedin_user_id();
    	$friends_and_me = implode( ',', (array) $friends );
    	$friends_and_me =  '&user_id=' . $friends_and_me;
    ?>
    <?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ).$friends_and_me.'&sort=DESC') ) : ?>
    

    Activity Loop

    much simpler and don’t have to worry about functions breaking. Subsequently, I believe if for some reason this solution doesn’t work for you and you prefer one of the earlier ones I suggested, then check out https://codex.buddypress.org/developer/loops-reference/the-activity-stream-loop/#adding-commenting-support as to fixing/supporting commenting


    modemlooper
    Moderator

    @modemlooper

    You can also use these two function so you don’t have to edit templates.

    function me_and_friends_filter_activity( $query ) {
    
      if ( empty( $query ) && empty( $_POST ) ) {
      
        $friends = friends_get_friend_user_ids( bp_loggedin_user_id() );
    	$friends[] = bp_loggedin_user_id();
    	$friends_and_me = implode( ',', (array) $friends );
    
    	$query = 'user_id=' . $friends_and_me ;
      }
      return $query;
      
    }
    add_filter( 'bp_ajax_querystring', 'me_and_friends_filter_activity', 999 );
    
    function me_and_friends_filter_activity_ajax( $query ) {
    
      if ( empty( $query ) ) {
      
        $friends = friends_get_friend_user_ids( bp_loggedin_user_id() );
    	$friends[] = bp_loggedin_user_id();
    	$friends_and_me = implode( ',', (array) $friends );
    
    	$query .= 'user_id=' . $friends_and_me ;
      }
      return $query;
    	
    }
    add_action( 'bp_legacy_theme_ajax_querystring', 'me_and_friends_filter_activity_ajax', 10);

    godavid33
    Participant

    @godavid33

    Thanks! I’m going to stick with my solution, just because I am already editing child-theme templates anyways, but I would probably throw my vote in for modemlooper’s solution over mine as a way to keep all modifications pretty centralized

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Adding a subnav with new page content to the profile/activity page’ is closed to new replies.
Skip to toolbar