1

I am using the BuddyBoss theme (BB child theme active) and need to control which menu appears based on whether the user is logged in or logged out. Logged out are granted access to a few URIs but most of the site is for members only. The default Buddy Boss menu is OK for logged in, but I wish to hide it for logged out users and replace it with a limited one without search or profile because I do not want them to have access to its search functionality (it's for a paid members site, logged out users have only very limited access). Could someone help with the PHP to accomplish? Where in the Buddy Boss theme does it go and how do I connect it to my two menus (both Template Headers in Elementor Pro)

Many many thanks in advance, I am new to PHP but am learning slowly with the help of this tremendous forum. Apologies if this question has been asked before, I have found a couple of similar answers but they did not work.

Tim.

3
  • You need to provide some code - are you using a child theme to modify the BuddyBoss theme? This actually isn't that difficult outright and you can take two approaches, the correct one and the shortcut. The correct one will require you to modify the file where the navigation is inserted into the page. The shortcut version would be to use CSS to hide the navigation items you don't want logged out users to see. I guess a third would be to use jQuery to .remove() links, but I'd advise against that as it opens up a whole mess. Commented Oct 13, 2020 at 21:07
  • Hi Tony. Thanks for your response. Makes Sense. I do not have code but can supply what is needed. I am indeed using the child theme for BuddyBoss. I would rather not just hide the search button with CSS, but remove it from a few pages, most importantly home. I have 2 headers, one is the buddy boss native header, the other an implentation of it based on their Elementor Header templates. Simple: <Logo, Log in, Log out.> My ideal scenario is to choose header based on logged in or logged out. This would solve everything as I am granting a few "inner" pages to non-logged in (w/ no search)
    – Tim
    Commented Oct 14, 2020 at 12:54
  • Here is the site on a beta subdomain, if that helps: beta.mojocircle.com
    – Tim
    Commented Oct 14, 2020 at 12:59

1 Answer 1

1

As you mentioned in the comments, if what works best for you is displaying different header files based on whether a user is logged in or not, this is the best way to achieve it:

if( is_user_logged_in() ) :
    /** This is the name of your second header file.
     * It assumes the 'header-' and the '.php' portions.
     * So the following get_header would use this file 'header-loggedin.php'. */
    get_header( 'loggedin' );
else :
    /* This would use the default header for your child theme named 'header.php'. */
    get_header();
endif;

Now if all that's different is the navigation, then what I'd do is just make two different navigation locations in the header and load them conditionally based on whether a user is logged in or not. Then, in the Menu Admin area in WP you have the ability to manage two different menus and simply assign one as 'main logged in' and 'main not logged in' or something like that.

The best solution really just depends on how many different aspects of the header you actually need. If it's just the nav, then I'd go with conditional menu locations. If it's more than just the nav, I'd go with conditional header loading.

Here's how you create two menu locations, add this to your functions.php:

register_nav_menus(
    array(
        /* Make sure you change the textdomain to match yor child themes. */ 
        'header-loggedin'       => esc_html__( 'Main Menu Logged In', 'tetdomain' ),
        'header-loggedout'      => esc_html__( 'Main Menu Logged Out', 'textdomain' )
    )
);

That creates two new menu locations that you can assign a menu to in the WP Appearances -> Menus screen.

Next, if going with a single header.php file, you place the following code where your navigation is supposed to appear:

if( is_user_logged_in() ) :
    wp_nav_menu(
        array(
            'theme_location'    => 'header-loggedin',
            'menu_id'           => 'header_loggedin'
        )
    );
else :
    wp_nav_menu(
        array(
            'theme_location'    => 'header-loggedout',
            'menu_id'           => 'header_loggedout'
        )
    );
endif;

As you can see, it uses the same is_user_logged_in() check and then simply establishes which 'menu location' should be used based on the result of the condition.

wp_nav_menu has lots of configurable options as well, so look into that in case you want to customize containers, etc.

8
  • Hi Tony, Creating two different header files is the best solution as I wish to use the same location, but different nav only. The issue is that BuddyBoss' Top menu is in 2 parts and the core part with the search is not an editable menu under the WP Menus section. I have managed - with your guidance - to add a menu to the top menu that is styled correctly, but it is replacing only the WP visible menu "Top", but leaving the search/sign in etc on the right intact!. So the job is to replace the whole top menu for logged out with it using the same styling, and use the other one for logged in.
    – Tim
    Commented Oct 15, 2020 at 9:55
  • I have screengrabbed the relevant code here if it helps: imgur.com/a/5kGZwxW Once again I truly appreciate your efforts here Tony. I need to get this resolved soon. The Ajax based search is superb in BuddyBoss, however it must be revealing data to non-logged in members, hence the need to remove it.
    – Tim
    Commented Oct 15, 2020 at 10:09
  • So the issue here is that the BuddyBoss theme is appending the search to the navigation via code - so what you need to do is remove that function/action within your child theme. You'll see in your header.php there's a do_action( THEME_HOOK_PREFIX . 'header' ); and somewhere in the theme there is a function that adds the search to that hook. You'll have to dig through the theme's files to find it and then use this method to remove it: wordpress.stackexchange.com/a/189066/60844 Commented Oct 15, 2020 at 13:49
  • You'll have to, when adding the remove_action() function, run the same check in your functions.php to determine whether the user is logged in or not. There's a note here about calling it too early and how to handle that: developer.wordpress.org/reference/functions/is_user_logged_in Commented Oct 15, 2020 at 13:53
  • Tony, thanks again for your advice. I don't feel sufficiently competent to make sure these changes will work and not break something and this is a project destined to go live fairly soon. I am going to study the PHP as a side project as I do want to understand it more. Any suggestions on a good book or site? For now, btw, this css is doing the job:
    – Tim
    Commented Oct 17, 2020 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.