Set default landing tab for BuddyPress Activity directory
If you are interested in changing the default landing tab( activity listing type ) for BuddyPress Activity directory, then you can do it easily. We are going to see it with an example where we will change the default landing tab from "All Members" activity to "My friends" activity.
All you need to do is put the following code in your bp-custom.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Set default activity directory tab for BuddyPress */ function buddydev_set_default_activity_directory_tab() { // If user is not logged in or // If the scope is already set, do not do anything ok. if ( ! is_user_logged_in() || isset( $_COOKIE['bp-activity-scope'] ) ) { return ; } // additional check for activity dir if ( ! bp_is_activity_directory() ) { return ; } $tab = 'friends'; // 'all', 'friends', 'groups', 'mentions' // Set scope to our needed tab, // In this case, I am setting to the 'friends' tab setcookie( 'bp-activity-scope', $tab,null, '/' ); $_COOKIE['bp-activity-scope'] = $tab; } add_action( 'bp_template_redirect', 'buddydev_set_default_activity_directory_tab' ); |
Here is a screenshot of the default activity tab:-
And here is what the user sees when they visit the activity directory for the first time(after you use the previous code)
As you noticed, the default landing tab is set to "My friends " and lists friends activity instead of the default "All members" activity.
Code Explanation:-
In the code we check if a user is logged in, the activity scope is not set and the current page is activity directory, then we set 'friends' as the default scope. It makes BuddyPress display friends activity on the default activity directory landing page instead of the All members activity. You can change the scope to any valid activity scope. The other available scopes are "groups" and "mentions".
Please do note that the default scope is only set if it was not already set earlier. If you are testing the code and you had visited activity directory earlier and had clicked on one of the tabs, your activity-scope would be already set. The code will only work for you when you clear the cookies.
Have fun working with BuddyPress 🙂
Hello,
Please can this code work with the buddypress follow plugin
Thank you
Hi Tosin,
It will work with any plugin that acts a tab on activity directory.
I also found out about this plugin https://fuc.wordpress.org/plugins/bp-whats-hot/
Will your code adapt to this
If that plugin ads a tab in the activity directory, It will work.
Hi Brajesh,
this code seems to work but does not really.
Initially, although the default tab is hilighted as set in the code (changed), the displayed content corresponds to the original default tab. Only after re-clicking the changed default tab, the proper content is loaded. Apparently, this issue is related to the fact that switching between the tabs is performed by javascript (not page reload).
Thank you Stefan,
I have just corrected the code. I had missed to add the last line(for current request keeping it in the cookie global).
Please check and do let me know if that works. There is no need for clicking. It all works based on the cookie.
Regards
Brajesh
Many thanks, Brajesh, now it works as expected.
In fact, I needed this for the BP Groups Directory to set "My Groups" as default tab. So I have modified your code:
I do not check whether the cookie was already set because I want to land on the new default tab on every visit, regardless of any previous views.
Regards,
Stefan.
Thank you Stefan.
Definitely helpful for people looking to change for groups. Appreciate you sharing here 🙂
Thank you
Brajesh