最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Change Navigation Bar based on logged in or not

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Oct 12, 2020 at 13:44 TimTim 112 bronze badges 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. – Tony Djukic 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 – Tim Commented Oct 14, 2020 at 12:59
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论