• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Load a different menu depending on whether the user is logged in or not in WordPress

Discover in this tutorial how to display a different menu depending on whether the user is logged in or not in WordPress, using code.

Being able to show or hide menu items depending on the user’s status is very handy, especially for membership sites.

In fact, if you access OsomCode without logging in you will see a menu:

main-menu-users-no-logged-WordPress

And when you have logged in with your account you will see another one:

main-menu-users-logged-in-WordPress

In a previous tutorial I explained how you can achieve this using the same menu and a bit of CSS.

In this case I will show you how to load different menus, which is much more appropriate if the menu between logged in and not logged in is very different.

Here we go:

Snippet to display a different menu to logged in users in WordPress

First of all, you must create two menus, one that is the one you want to show to logged in users, and another one to non-logged in users.

non-logged-users-menu-WordPress
Menu for non-logged in users

Once you have it, add the following snippet to the functions.php file of your theme:

To see this and another 839 code snippets of this website, login or subscribe here.

In this snippet you use the wp_nav_menu_args filter in combination with the is_user_logged_in conditional tag to display one menu or another depending on whether the user is logged in or not.

In this case, the menus are called “User logged in” and “User not logged in” but if you want you can use another nomenclature. If you do so, you will only have to replace it in the snippet.

The code works for both Genesis child themes and other WordPress themes.

Conclusions

You can see here how easy it is to load a specific menu depending on whether or not the user is logged into WordPress.

Any questions? I read you in the comments.

And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉

Header Menu PHP WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?