Skip to:
Content
Pages
Categories
Search
Top
Bottom

Is it possible to make Activity and Forum profile tabs private?

  • @fearthemoose

    Participant

    I’ve been trying to find a way to make the activity and forum tabs only visible to the logged in user. So for example, if im logged into my profile, i will be able to see/use the activity and forum tabs, however, im the only one who can see them.

    I managed to hide the tabs from users using css, but that only works for people who are logged out. Anyone who is logged into their account can still see those tabs due to there not being a specific css class. However, i would much rather a non CSS way of removing them for obvious reasons.

    Any help would be much appreciated, as i cant seem to find to much on the topic bar removing it with css.

    Cheers

Viewing 7 replies - 1 through 7 (of 7 total)
  • @rezbiz

    Participant

    Super interested to learn the apparently much-guarded truth to this question. It’d be great to be able to toggle which links are visible on a navigation bar based on whether the visitor is logged in or not.

    @danbp

    Participant

    @fearthemoose, @rezbiz,

    Here “the best guarded secret” finally revealed !
    But it’s not a secret, is part of Codex and a multi handled topic subject on this forum.

    What we want to do: remove access possibility.
    Where: on BuddyBar navigation menu (the one below the profile header).
    Specifics: make the activity and forum tabs only visible to the logged in user.

    NOTE: The activity tab is shown by default when you visit a profile.

    As we are going to hide/remove this tab to visitors, we need to define as first another default tab. If we won’t do that, the visitor will get a Page not found error. Which is not exactly the case here, the page exist but the visitor can’t access it.

    Let’s define arbitrary Profile as the new default tab. You can of course define what ever other existing tab on your BuddyBar as default.

    Add this line to bp-custom.php
    define( 'BP_DEFAULT_COMPONENT','profile' );

    Now the mega “open secret” !

    The folowing snippet contains almost any BP menu items you can have on a BuddyBar, and includes also the Forum item, in case you use bbPress.
    You can remove or comment out those you don’t want to use.
    Note also that it is only an example – you have to play with it to fit your need. But at least you have the right syntax to use.
    Note also that some profile menu items are user only, such as message or notice. In other words, these are always private.

    This function goes preferably to bp-custom.php

    function bpfr_hide_tabs() {
    global $bp;
    	 /**
    	 * class_exists() & bp_is_active are recommanded to avoid problems during updates 
    	 * or when Component is deactivated
    	 */
    
    	if( class_exists( 'bbPress' ) || bp_is_active ( 'groups' ) ) :
            
            /** here we fix the conditions. 
            * Are we on a profile page ? | is user site admin ? | is user logged in ?
            */
    	if ( bp_is_user() && !is_super_admin() && !is_user_logged_in() ) {
    
            /* and here we remove our stuff ! */
    		bp_core_remove_nav_item( 'activity' );
    		bp_core_remove_nav_item( 'friends' );
    		bp_core_remove_nav_item( 'groups' );
    		bp_core_remove_nav_item( 'forums' );
    	}
    	endif;
    }
    add_action( 'bp_setup_nav', 'bpfr_hide_tabs', 15 );

    Some other hints for privacy
    if you want to allow only the profile owner to view some tabs, replace
    !is_user_logged_in by !bp_is_my_profile() – this scenario doesn’t need a check for logged in users as it is made by bp_is_my_profile.

    If you want only to make a profile private, read here.

    If you want to remove a sub-nav item (ie. View on Profile), you use something like:
    bp_core_remove_subnav_item( 'profile', 'view' );

    Many other possibilities for navigation are also available, including for groups. Several examples are given on Codex, here.

    And if not enough, RTFM and search the forum ! 🙂

    Have fun !

    @rezbiz

    Participant

    You’re the man Dan!

    Super useful info. I’m still an uber plebe in all this, so please forgive me for the further clarification:

    So let’s say I create a page, then put a link to that page in the top menu bar (using Kleo theme for further context). This page isn’t a BP page at all, just a regular page with some Visual Composer elements. Is there a way I can make this regular page link in the navigation menu dependent on the visitor’s logged in status?

    You may have already answered this in the info above, if so please just tell me to take a closer look at your reply. I’m not opposed to putting in the footwork to understand the basics, just don’t know enough to know what I don’t know. ‘Namsayin?

    @danbp

    Participant

    Hi @rezbiz,

    in brief, you want to add some status dependent custom link to the BuddyBar.

    I’ll ignore Kleo (premium theme) and Visual Composer (third party plugin).

    Following examples let you add an external link to a non BP page on almost any menus (except BP usermenu).

    How it works ?
    – create a test page and call it Book.
    – the page slug must be /book/
    – the link (tab/button) will be displayed to logged-in users only.

    I made also a snippet which add such a link to the WordPress Main Menu. This menu is the one you see in WP’s menu customizer by default. Depending the theme you use, you have always a main menu and X others. This can vary and it’s to you to find how it can/should work eventually. See theme documentation and WP Codex if you get in trouble with it.
    Note that it should work with all Twenty themes at least.

    Add the snippets to bp-custom.php and see the result. After that, read through the code and modify the page name/ID and slug to your need. Leave anything else untouched.

    
    // add Custom tab on site wide activity and members directory pages
    function tab_custom_link_to_page() {
    
    	if ( bp_is_activity_component() || bp_is_page( BP_MEMBERS_SLUG ) ) {
    
    		if ( !is_user_logged_in() )
    			return false;
    
    		// link to what you want
    		$link = bp_get_root_domain() . '/book';
    
    		echo'<li><a href="'. $link .'">My Book</a></li>'; 
    	}
      
    }
    add_action( 'bp_activity_type_tabs', 'tab_custom_link_to_page' ); // site wide activity page
    add_action( 'bp_members_directory_member_types', 'tab_custom_link_to_page' ); // members directory page
    
    // add Custom tab on Profile Nav
    function add_profiles_link_to_page(){
    
    	if ( bp_is_profile_component() || bp_is_user() ) {
    
    	if ( !is_user_logged_in() )
    		return false;
    
    	$link = bp_get_root_domain() . '/book/';
    
    	?>
    		<li><a href="<?php echo $link; ?>"><?php printf( 'My Book' ); ?></a></li>
    	<?php  
    	}
    }
    add_action( 'bp_member_options_nav', 'add_profiles_link_to_page' );
    
    // add Custom tab on Groups Nav
    function add_groups_link_to_page(){
    
    if( bp_is_active( 'groups' ) ) {
    
    	if ( !is_user_logged_in() )
    		return false;
    
    	$link = bp_get_root_domain() . '/';
    
    	bp_core_new_subnav_item( array(
    		'name'			=> 'My Book',
    		'slug'			=> 'book',
    		'parent_slug'		=> bp_get_current_group_slug(),
    		'parent_url'		=> $link,
    		'screen_function'	=> true, 
    		'position'		=> 40 ) );
    	}
    }
    add_action( 'bp_setup_nav', 'add_groups_link_to_page' );
    
    // add Custom tab on Groups Directory
    function add_groups_directory_link_to_page() {
    
    	if( bp_is_active( 'groups' ) ) {
    
    	if ( !is_user_logged_in() )
    		return false;
     
        if ( bp_is_current_action( 'custom' ) ) 
            return;
     
    	$link = bp_get_root_domain() . '/book';
    
        $button_args = array(
            'id'         => 'my-books',
            'component'  => 'groups',
            'link_text'  => 'My Book',
            'link_title' => 'My Book',
            'link_class' => 'my-books no-ajax',
            'link_href'  => $link,
            'wrapper'    => false,
            'block_self' => false,
        );  
       
        ?>
    		<li><?php echo bp_get_button( apply_filters( 'bp_get_group_custom_button', $button_args ) ); ?></a></li>
        <?php
    	}	
    }
    add_action( 'bp_groups_directory_group_filter', 'add_groups_directory_link_to_page' );
    
    // add Custom tab on WP Main Menu ( after Home, About, etc )
    function main_menu_custom_link_to_page( $menu ) { 
         
    	if ( !is_user_logged_in() )
    			return $menu;
    	else
     	// link to what you want
    	$link = bp_get_root_domain() . '/book';
    
    	$grouplink = '<li><a href="'. $link . '">My Book</a></li>';
    
    	$menu = $menu . $grouplink;
    	return $menu;
    }
    add_filter( 'wp_nav_menu_items', 'main_menu_custom_link_to_page' );

    Yes, it looks complicated, but it isn’t. Simply it’s not homogeneous ! 🙂

    @nffox

    Participant

    One question. I’m trying to remove some menu items, but it only works if I add 2 different codes to functions.php and bp-custom.php.

    This one goes to functions.php

    function exclude_tab_if_not_personal_profile() {
        if ( ! bp_is_my_profile() ) {
            bp_core_remove_nav_item( 'friends' );
        }
    }
    add_action( 'bp_init', 'exclude_tab_if_not_personal_profile' );

    And this one to bp-custom.php

    function bp_remove_nav_item() {
        global $bp;
        bp_core_remove_subnav_item( $bp->profile->friends, 'change-avatar' );
    }
    add_action( 'wp', 'bp_remove_nav_item' );

    >>>>>>>>>>>>>>>
    It works… but

    1. The “friends” tabs appears in the profile (top navbar), and I just need it to appear in the profile menu (sidebar).

    2. Adding this piece of code for removing MORE items do nothing.
    The combination I used for bp-custom.php.

    function bp_remove_nav_item() {
        global $bp;
        bp_core_remove_subnav_item( $bp->profile->friends, 'change-avatar' );
        bp_core_remove_subnav_item( $bp->profile->forums, 'change-avatar' );
        bp_core_remove_subnav_item( $bp->profile->media, 'change-avatar' );
    }
    add_action( 'wp', 'bp_remove_nav_item' );

    and for functions.php

    function exclude_tab_if_not_personal_profile() {
        if ( ! bp_is_my_profile() ) {
            bp_core_remove_nav_item( 'friends' );
            bp_core_remove_nav_item( 'forums' );
            bp_core_remove_nav_item( 'media' );
        }
    }
    add_action( 'bp_init', 'exclude_tab_if_not_personal_profile' );

    Thanks, I would really appreciate any help, as apparently none of solutions found on this forum worked for me.

    @nffox

    Participant

    and thank you beforehand, @danbp

    @9jablazejams

    Participant

    wow it was help full</a

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.