If you have a WordPress site and it requires your users to log in then you may need to provide your logged-in users extra menus which you don’t want to show the users who are not registered.
For example, you may want to provide the user’s profile menu or log-out menu to your logged-in users but you don’t want to show that menu to the people who do not have a profile on your website. On the other hand, you don’t want to show the login menu to a user who has already logged in. That’s the reason you may likely need to have a dynamic navigation menu that displays different menu items to users depending on whether they’re logged out or logged in.
Although, WordPress, as a CMS is getting more attractive in its new versions and also, has the option to create multiple menus. Unfortunately, there are no conditions to make it visible only for logged-in users. In this article, we will show you two different ways (using code and plugin) to show different menus to logged-in users in WordPress.
For showing two different menus to your logged-in and the logged-out user you need to create two different menus.
Go to “Appearance > Menus”, and first create a menu for logged-in users.
Then create another menu for the logged-out users with the menus you want to show them.
Now, it’s time to add code to your theme’s function.php file. To do that, open up your favorite FTP Client Software like FileZilla. Go to the WordPress install’s root folder, look for wp-config.php, right-click, then select “View / Edit.”
After opening the function.php file, add the following code:
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' );
Now, you can see your menus are showing differently for your logged-in and logged-out visitors.
For this method, you don’t need to create different menus. Just install and activate the “Nav Menu Roles” plugin, and go to “Appearance > Menus”. Now, create only one menu where you need to add all the menus you want to show to your logged-in and logged-out users.
Now, expand a menu and you will see three options in “Display Mode”. Expand all menus one by one and select who you want to show the menu. You can even restrict menus for your user’s role with this plugin.
Once you choose the menus for logged-in and logged-out users save the menu.
Now you can check, your logged-in visitors will see the logged-in menu, and non-registered or logged-out users will see a different menu.
Whether you used the code or the plugin, we hope this article helped you set different navigation menus for your logged-in and logged-out users. For any further queries feel free to comment down below.
If you want to know how to add a log-out link in the navigation menu for your logged-in users, please check this article. Also, following these tutorials, you can create a login form for your logged-out users and add a modal login form to your WordPress site. Visit our Blog page for more and more WordPress-related tutorials. Also, please let us know if you want any specific tutorial related to WordPress. We will be very happy to write the tutorial for you.