When running a WordPress membership site, a WordPress forum, or allowing users to submit posts, you may run into situations where you want to display different navigation menus to logged in and out users. For example, you might want to show users who are logged in a link to your profile and a link to login or register for users who are not logged in. In this article, we will show you how to display different menus to logged in users. WordPress.
Creating multiple menus in WordPress
In WordPress, even if your theme has one menu location, you can still create multiple menus for the same location.
Option 1) Go to Appearance » Menus, create two menus with logged in and logged out. Create a menu named “logged-in” and create another named “logged-out”.
After creating the menus, add the following code to your theme’s functions.php file
function my_wp_nav_menu_args( $args = '' ) { if( is_user_logged_in() ) { $args['menu'] = 'logged-in';} else { $args['menu'] = 'logged-out';} return $args;}add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
That’s it, logged in visitors will see the “logged-in” menu and unlogged or logged out users will see the “logged-out” menu
This method allows you to create two different menus for your users, so you can freely update your menus for online or offline users. There are other ways to do the same thing. For example, if you only want to add login and logout links to your navigation menu, you need to add custom menu items in the WordPress navigation menu.
Option 2) While there are different plugins to create a dynamic navigation menu, we recommend using the free User Menus plugin from Jungle Plugins.
The plugin provides three main sets of features that are useful for creating dynamic menus:
- Visibility controls for menu items, for example, who can see each menu item (everyone, offline users, online users, specific user roles)
- User information recorded in the navigation menu, for example, “Hello, John Doe.”
- Log in and log out links for navigation menu
We hope this article helped you display different menus to registered users on your WordPress site. Remember that if you have your website hosted in Plhuit we can take care of doing this for you. If you are not yet a Phluit user, check out our plans.