wordpressbuddypressusertype

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


Solution

  • 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' );