Skip to:
Content
Pages
Categories
Search
Top
Bottom

how to combine activity tabs to one


  • Antonios
    Participant

    @take0n

    Hi,

    I would like to combine all activity tabs (Personal, Mentions, Favorites, Friends) to one page.
    I am able to use CSS to hide those tabs but I would like to know how to put them all in to one general tab named “All” or have it like that by default.

    I would also like to restrict those to only members that are friends and not to everybody but this is already in the forums.

    Thank you

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

  • Venutius
    Moderator

    @venutius

    Hi there,

    My suggestion is that you add a new menu item “All” and create a new set of queries for the page then make that page the default landing page for the profile activity page. Here’s my code to do this:

    function bpex_set_member_default_nav() {
     
    		if ( ! bp_is_user_profile() && ! bp_is_user_activity() && ! bp_is_user() ) {
    		    return;
    	         }
    
                    bp_core_new_nav_default(
    		array(
    			'parent_slug'       => buddypress()->activity->id,
    		// other "activity" sub_nav slugs : personal favorites friends groups mentons
    			'subnav_slug'       => 'all-activity',
    			'screen_function'   => 'bp_activity_screen_all_activity'
    		)
    	);
    }
    add_action( 'bp_setup_nav', 'bpex_set_member_default_nav', 20 );
    
    add_action( 'bp_setup_nav', 'bpex_just_me_tab', 50 );
    add_action( 'bp_setup_admin_bar', 'bpex_admin_bar_add', 50 );
    
    function bpex_just_me_tab() {
    	
    	if ( ! bp_is_user_profile() && ! bp_is_user_activity() && ! bp_is_user() ) {
    		return;
    	}
    	
    	global $bp;
    	$user_id = bp_displayed_user_id();
    	
    		bp_core_new_subnav_item( array(
    			'name'            => _x( 'All', 'Profile activity screen sub nav', 'buddypress' ),
    			'slug'            => 'all-activity',
    			'parent_url'      => bp_core_get_user_domain( $user_id ) . 'activity/',
    			'parent_slug'     => 'activity',
    			'screen_function' => 'bp_activity_screen_all_activity',
    			'position'        => 10
    		) );
    
    }
    
    function bpex_admin_bar_add() {
    	
    	global $wp_admin_bar, $bp;
    
    	if ( ! bp_use_wp_admin_bar() || defined( 'DOING_AJAX' ) ) {
    		return false;
    	}
    
    	$user_id = get_current_user_id();
    	
    	if ( ! $user_id || $user_id == 0 || ! is_numeric( $user_id ) ) {
    		return;
    	}
    	
    	// Personal.
    	//$wp_admin_bar->remove_menu( 'my-account-activity-personal', 'my-account-activity' );
    
    	$wp_admin_bar->add_menu( array(
    		'parent'   => 'my-account-activity',
    		'id'       => 'my-account-activity-all-activity',
    		'title'    => 'All',
    		'href'     => bp_core_get_user_domain( $user_id ) . 'activity/all-activity/',
    		'position' => 10
    	) );
    }
    function bp_activity_screen_all_activity() {
    
    	do_action( 'bp_activity_screen_all_activity' );
    
    	bp_core_load_template( apply_filters( 'bp_activity_template_all_activity', 'members/single/home' ) );
    }
    
    function bp_activity_filter_all_activity_scope( $retval = array(), $filter = array() ) {
    
    	// Determine the user_id.
    	if ( ! empty( $filter['user_id'] ) ) {
    		$user_id = $filter['user_id'];
    	} else {
    		$user_id = bp_displayed_user_id()
    			? bp_displayed_user_id()
    			: bp_loggedin_user_id();
    	}
    
    	// Should we show all items regardless of sitewide visibility?
    	$show_hidden = array();
    	if ( ! empty( $user_id ) && $user_id !== bp_loggedin_user_id() ) {
    		$show_hidden = array(
    			'column' => 'hide_sitewide',
    			'value'  => 0
    		);
    	}
    
    	// Determine groups of user.
    	$groups = groups_get_user_groups( $user_id );
    	if ( empty( $groups['groups'] ) ) {
    		$groups = array( 'groups' => 0 );
    	}
    
    	// Determine the favorites.
    	$favs = bp_activity_get_user_favorites( $user_id );
    	if ( empty( $favs ) ) {
    		$favs = array( 0 );
    	}
    
    	// Determine friends of user.
    	$friends = friends_get_friend_user_ids( $user_id );
    	if ( empty( $friends ) ) {
    		$friends = array( 0 );
    	}
    
    	$retval = array(
    		'relation' => 'OR',
    		array(
    			'relation' => 'AND',
    			array(
    				'column'  => 'user_id',
    				'compare' => 'IN',
    				'value'   => (array) $friends
    			)
    		),
    		array(
    			'relation' => 'AND',
    			array(
    				'column' => 'component',
    				'value'  => buddypress()->groups->id
    			),
    			array(
    				'column'  => 'item_id',
    				'compare' => 'IN',
    				'value'   => (array) $groups['groups']
    			)
    		),
    		array(
    			'relation' => 'AND',
    			array(
    				'column' => 'user_id',
    				'value'  => $user_id
    			)
    		),
    		array(
    			'relation' => 'AND',
    			array(
    				'column'  => 'id',
    				'compare' => 'IN',
    				'value'   => (array) $favs
    			)
    		),
    		array(
    			'relation' => 'AND',
    			array(
    				'column'  => 'content',
    				'compare' => 'LIKE',
    
    				// Start search at @ symbol and stop search at closing tag delimiter.
    				'value'   => '@' . bp_activity_get_user_mentionname( $user_id ) . '<'
    			)
    		),
    		// We should only be able to view sitewide activity content for friends.
    		array(
    			'column' => 'hide_sitewide',
    			'value'  => 0
    		),
    
    		// Overrides.
    		'override' => array(
    			'filter'      => array( 'user_id' => 0 ),
    			'show_hidden' => true
    		),
    	);
    	
    
    	return $retval;
    }
    add_filter( 'bp_activity_set_all-activity_scope_args', 'bp_activity_filter_all_activity_scope', 10, 2 );

    Once you have done that you would also want to overload the buddypress/bp-templates/your-template/members/single/activity.php to make sure this new page contains the post update form:

    Change:

    if ( is_user_logged_in() && bp_is_my_profile() && ( !bp_current_action() || bp_is_current_action( 'just-me' ) ) )
    	bp_get_template_part( 'activity/post-form' );

    To:

    if ( is_user_logged_in() && bp_is_my_profile() && ( !bp_current_action() || bp_is_current_action( 'all-activity' ) ) )
    	bp_get_template_part( 'activity/post-form' );
    

    Note: with this method there is a danger of duplicating activity since a friend posting to a group would show up in the groups search as well as the friends search etc. so you may want to adjust the $retval criteria.

    Also you might want to add a post form for visiting users so they can leave a comment for the user, you could ad this:

    function bpex_load_mention_me() {
    	
    	if ( get_current_user_id() != bp_displayed_user_id() ) {
    		$_GET['r'] = bp_activity_get_user_mentionname( bp_displayed_user_id() );
    		bp_get_template_part( 'activity/post-form' );
    	}
    
    }
    
    add_action( 'bp_before_member_body', 'bpex_load_mention_me' );

    Venutius
    Moderator

    @venutius

    I’ve updated this code with more options


    Achim
    Participant

    @ar7

    I will try that, sounds very promising – thank you! 🙂


    btees
    Participant

    @btees

    Hi @venutius,

    I really like your code.

    I’ve been trying to a custom metavalue I created to your bp_activity_filter_all_activity_scope function.

    So far it looks like:

    $industry = xprofile_get_field_data( 2, $user_id, false );

     array(  
            'meta_query' => array(
                array('meta_key' => 'bpcat',
    			'meta_key' => $industry))
    			
        ),

    The activity posts can be tagged with an industry, then the aim is to have the activities show up in the feeds of people in that industry.


    Venutius
    Moderator

    @venutius

    I’ve combined this code into a plugin with some extra options: https://wordpress.org/plugins/bp-profile-activity-wall/


    Venutius
    Moderator

    @venutius

    @btees you are not setting this up correctly, when you use the meta_query, you need to specify the key (not meta key) and the value (not meta_value).

    array(  
        'meta_query' => array(
            'key' => 'bpcat',
    	'value' => $industry
        )	
    ),

    btees
    Participant

    @btees

    Hi @venutius, thanks for the reply.

    Sorry, I used that at first and then changed to meta_key and meta_value in a stupor after hours of not figuring it out.

    I feel like my issue may be figuring out how to properly nest the array values. It’s so frustrating because I feel like I’m always just on the cusp of it!

    So at the moment the start of my code looks like:

    $retval = array(
    		'relation' => 'OR',
    		 array(
    			'relation' => 'AND',
    			array(
    				'column'  => 'user_id',
    				'compare' => 'IN',
    				'value'   => (array) $friends
    			)
    		),
    		array(
    			'relation' => 'AND',
    		 array(
    		 'meta_query' => array(
    		'key'     => 'bpcat',
                    'value'   => 'image',
    				) 
    				)
    		),

    For the moment I’ve set the metavalue to the static value of ‘image’ to try to get my loop working before adding any further complexity


    btees
    Participant

    @btees

    I’ve achieved what I wanted. Most likely in an incredibly convoluted way.

    If anyone can see any glaring issues with my code please shout. I’m a ludite when it comes to querying databases, arrays, etc.

    // Create the array for post ids related to the users industry
    		
    	global $wpdb;
    	$industry = xprofile_get_field_data( 2, $user_id, false );
    	$metas = $wpdb->get_results ( "SELECT activity_id FROM wp_bp_activity_meta WHERE meta_value='$industry'" );
    	$metasjson = json_decode( json_encode($metas), true);
    	
    		
    	$newList = array();
    foreach($metasjson as $key=>$listItem) {
     $newList[$key] =  $listItem['activity_id'];
    
    } 

    Then I added the below to your $retval

    // Get Posts in Specified Category
    		array(
    			'relation' => 'AND',
    			array(
    				'column'  => 'id',
    				'compare' => 'IN',
    				'value'   => (array) $newList
    			)
    		),

    Venutius
    Moderator

    @venutius

    Have you got error logging on? What errors are you seeing?

    One thing I can see from a quick glance:

    $metas = $wpdb->get_results ( "SELECT activity_id FROM {$wpdp->prefix}bp_activity_meta WHERE meta_key = $key AND meta_value = $industry" );

    In this case $industry must be a single value string, which I think is correct.


    jennifersanchez
    Participant

    @jennifersanchez

    Hi! Great post!

    But i only need that the user only can be the activity of his friends and group and not to all the community.

    Any suggest? i cant found a solution for it.
    Thanks!!!


    jennifersanchez
    Participant

    @jennifersanchez

    i found it!!

    array(
    			'column' => 'hide_sitewide',
    			'value'  => 1
    		),

    Antonios
    Participant

    @take0n

    WOW, I mean wow!

    Thank you so much all for all the input and solutions you provided! I’ve been off for a while and was astonished by all your replies!

    Thank you very, very much! I will try this out today and let you know of my results as well 🙂

    Thank you!

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.
Skip to toolbar