Skip to:
Content
Pages
Categories
Search
Top
Bottom

Hiding nav item not working as expected


  • panosa1973
    Participant

    @panosa1973

    WordPress 4.9.5 -> BuddyPress 2.9.4 -> Agama Blue Child Theme

    I ended up with the below code from posts in these forums but it doesn’t do quite what I need it to do.
    Users should not be able to see other users’ friends so the Friends tab when viewing another user’s profile page must be removed along with the functionality. Here’s my code:

    function bpfr_hide_tabs() {
    	if ( !is_super_admin() && !bp_is_my_profile() ) {
    		bp_core_remove_subnav_item( 'profile', 'friends' );  // This does nothing
    		//bp_core_remove_nav_item( 'friends' );  //Or this but this also hides the Friends main menu item, not just the Friends tab in the user's profile page 
    	}
    }
    add_action( 'bp_setup_nav', 'bpfr_hide_tabs', 15 );

    I’ve switched between both options in the code. bp_core_remove_subnav_item( 'profile', 'friends' ); doesn’t really do anything and bp_core_remove_nav_item( 'friends' ); does remove the Friends tab from another user’s profile but it also removes it from the main menu which is a different thing. The main menu Friends link points to my own Friends whereas the Friends tab in another user’s profile points to their friends. Any help would be appreciated.

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

  • Venutius
    Moderator

    @venutius

    I think what you’d need to do would be to add a check to see if the user was viewing their own profile or not, then choose to execute the code or not.

    so something like:

    global $bp;
    if ($bp->loggedin_user->id; != $bp->displayed_user->id; ) {
        //execute code;
    }

    panosa1973
    Participant

    @panosa1973

    Hi @venutius. This works the same as my code. Say I’m a regular member viewing another member’s profile. The issue is that when it detects that it’s not my profile page and it removes the other member’s Friends tab, it also removes my own Friends menu item from my Main Menu. So basically when I’m viewing someone else’s profile, I don’t have the link to my main menu to visit my own Friends page because bp_core_remove_nav_item( 'friends' ); removes that too.


    Venutius
    Moderator

    @venutius

    I see, sounds like this is going to be difficult to do using this method, since both items use the same nav.


    Venutius
    Moderator

    @venutius

    There’s another option, when adding a nav item using bp_core_new_nav_item() there is this setting:

     *     @type bool        $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
     *                                                member profile other than your own. Default: true.

    So if you remove then re-register the frends nav, you can specify if it should only be displayed when the user is viewing their own profile.


    panosa1973
    Participant

    @panosa1973

    Thank you. I’ll give it a try.


    panosa1973
    Participant

    @panosa1973

    @venutius you were right on the money! This worked. Now a member can see their Friends menu item even when they’re on another member’s profile but without having access to the other member’s friends. And you said it was gonna be difficult. Hah! 🙂
    Thank you yet again!

    function hide_friends_tab() {
    	global $bp;
    	if ( !bp_is_my_profile() && !is_super_admin() ) {
    		bp_core_remove_nav_item( 'friends' ); 
    
    		bp_core_new_nav_item( 
        	        array( 
                    'name' => __('Friends', 'buddypress'), 
                    'slug' =>  $bp->friends->slug, 
                    'position' => 50, 
                    'show_for_displayed_user' => false,
                    'default_subnav_slug' => 'friends'
        ));
    	
    	}
    }
    add_action( 'bp_setup_nav', 'hide_friends_tab', 15 );
Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar