How To Display A Different Nav Menu For Logged In Members & Logged Out Visitors

If you’re using WordPress as a membership site, you may want to display a special navigation menu to logged in members.

There’s different ways to do this using either CSS classes or PHP code.

Custom functions are the best way to go in my opinion because you don’t have to worry about adding more CSS to your child themes files and slowing down your site. PHP code is more powerful and efficient. You also don’t need to worry about cross browser issues.

To display a different nav menu for logged in and logged out users, you firstly need to create 2 menu’s.

I won’t show you how to do that because its so easy even your mum could do it!

Create 2 nav menus

  1. One for Logged in members
  2. And one for logged out visitors

Here’s what they look like once you’ve created them.

nav menus

You can then select the non-members menu as the primary menu and leave the members menu unassigned.

You’d probably want to include a registration link in the non members nav menu but not in the members menu.

Paste Code In Functions File

This is the code you need to paste into your child themes functions.php file.

You only need the first code snippet above if you are using the same menu location for logged in members and logged out non members.

If you want to display another menu for members in a different menu location, you’ll need to use both code embeds above together and change the menu location in the code which is currently set to display the secondary menu for logged in members.

Different Menu Links For Logged In Members

Another way to display a different menu for logged in members is to include a custom menu link item which is added to the primary or secondary nav menu for logged in members. When logged out, the link is not added to the menu.

Replace the url in this code with your own menu link and change the primary to secondary if needed.

Here’s some code which you can use to display the nav menu for logged in members in a different location like the footer for either the primary or secondary nav menu’s.

Nav Menu Roles Plugin

Here’s a plugin which pretty much does the same thing as the code snippets above.

This plugin enables you to hide menu items based on user roles. You can:

  • Limit display of this menu item to users who are logged in or out, or customize by role.
  • Limit display of this menu item to users of selected roles.

menu access roles

Nav Menu Items Visibility Plugin

This plugin enables you to add conditional tags to each nav menu item so you can hide or display each menu link based on specific conditions.

menu item visibility

The conditional tag to hide menu items for logged in users is:

is_user_logged_in()

That’s all folks!

I know there are other ways to display different menus for logged in and logged out users however these are the best in my opinion.

How about you? Do you have a better solution than this one?

Related Posts

Never Miss a Tip from Brad Dalton

Get my latest web design tutorials, elegant coding solutions & useful tips.

5 Reasons Mad Mimi Works Better

mad-mimi
  1. Sends From Your Email Address - More personal & higher open rate
  2. Cheapest - $42 for list of 10,000 emails, unlimited sends
  3. Ease of Use - The easiest system to create & setup campaigns
  4. RSS to E-Mail - Auto send list of latest posts
  5. Auto Send Any Number of Days - enables you to offer multiple newsletters at different frequencies.

Click here to create a free account.

Comments

  1. Surendra Soni says

    Hello Sir,

    How can I implement the same code with custom menu location like we use Top Header Area to show our custom Menu.

    Regards,

    Surendra

  2. Rob says

    Hi Brad,

    This looks just like the sort of thing I’m trying to do, but I would like to change the top menu of my site rather than the main nav menu. I’m kinda new to all this, and was wondering how I could alter your code to keep the main nav the same but change it up top?

    Thanks in advance.

    • Brad Dalton says

      You’ll need to use both snippets and make sure the menu locations in the code and settings are correct otherwise it will not work.

  3. Martin says

    Your code for the 2 nav menus is excellent.

    Anyway my failed approach to this was to using two different menus by registering two nav menu-locations, logged-in and logged-out. Also I’m assigning a menu to each of them. In my functions.php I’m declaring a function like ‘display_main_menu(…)’, which calls wp-nav-menu with either the first, or the second menu-location as an argument, depending on login-status. But i couldn’t get this method to work.

    I don’t like the plugin so i’m going to use your code instead.

    • says

      The code works perfectly and has been tested. The reason it might not work for you is it includes 2 code snippets in the one paste. The second one is for use if you have a menu in a different location. Otherwise, use the first block of code.

      Did you read this: You only need the first code snippet from this block if you are using the same menu location for logged in members and logged out non members. Example: The primary menu.

      If you are using both the primary and secondary nav menu, then you should use the second block of code.

      I included both options because some people like to use use a different menu location for logged in members and logged out visitors.

        • says

          Yep. Most people will use the first block when using the same menu location for logged in and logged out users. Otherwise you’ll need the first 2 blocks of of code if you want the menu to display in different locations for logged in and logged out users.

Leave a Reply

Your feedback is always appreciated.

Your feedback is always welcome & appreciated however WP Sites does not reply to anonymous comment authors or loaded questions. Members get answers to unlimited questions.