Change the default Buddypress user landing page differently depending on the type of user
Solution 1:
I found a solution.
First, here's how I created a new navigation item; I put this code in my bp-custom.php
file:
function bp_custom_user_nav_item() {
global $bp;
$args = array(
'name' => __('newnavitem', 'buddypress'),
'slug' => 'newnavitem',
'default_subnav_slug' => 'newnavitem',
'position' => 0,
'show_for_displayed_user' => true,
'screen_function' => 'bp_custom_user_nav_item_screen',
'item_css_id' => 'newnavitem'
);
bp_core_new_nav_item( $args );
}
add_action( 'bp_setup_nav', 'bp_custom_user_nav_item', 99 );
After I created a new directory in community/members/single in my theme directory, and in this directory I will put my newnavitem loop and edit template files.
And I linked the newnavitem and its template with this function
function bp_custom_user_nav_item_screen() {
add_action( 'bp_template_content', 'bp_custom_screen_content' );
bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'members/single/newnavitem' ) );
}
And here's how I disabled the current nav item for certain user types, and set it as default landing page for another certain user types.
function conditionally_disable_newnavitem( $enabled, $component) {
if (user_can(bp_displayed_user_id(),'firstusertype') && $component === 'newnavitem') {
$enabled = false;
}
return $enabled;
}
add_filter( 'bp_is_active', 'conditionally_disable_new_navitem', 10, 2 );
function set_default_component () {
if ( user_can(bp_displayed_user_id(),'secondusertype') || user_can(bp_displayed_user_id(),'thirdusertype')) {
define ( 'BP_DEFAULT_COMPONENT', 'newnavitem' );
add_filter( 'bp_is_active', function($retval, $component){
if($component === 'newnavitem') return true;
return $retval;
}, 10, 2 );
} else {
define ( 'BP_DEFAULT_COMPONENT', 'activity' );
}
}
add_action( 'bp_core_setup_globals', 'set_default_component' );