简体   繁体   中英

Change the default Buddypress user landing page differently depending on the type of user

I made a custom navigation page for users, using the plugins.php file. But I would like this page/option to be only available for some types of users; and make it the default landing page for them.

I can't figure out how to that.

I tried to make it the default landing page, and in the plugins template file, adding a condition that redirect users depending on their type... but redirection doesn't work by there it seems.

Any clue, plz?

I'm using Wordpress 5.8.2 and Buddypress 9.1.1. Thanks

I found a solution.
first, Here's how I created a new navigation item. I put theese codes 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 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 conditionnaly_disable_newnavitem( $enabled, $component ){
    if ( user_can(bp_displayed_user_id(),'firstusertype') && $component === 'newnavitem') {
        $enabled = false;
    }
    return $enabled;
}

add_filter( 'bp_is_active', 'conditionnaly_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' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM