WordPress Quick Tip — How To Show Different Menu To Logged In Users

WordPress menus offer a lot of customization features. Adding Login / Logout link to your WordPress navigation menus for instance is super cool. We have already shared the WordPress tip on how to add login / logout link to WordPress menu in the previous post. How about tweaking the WordPress menus a little more for showing different menu to logged in users!

As a developer you might come across the client’s requirement where he wants to display different menu items to users based on their login status. For instance, if a user is logged in, WordPress should display a menu with multiple options / functionality or else if the user is a visitor,  WordPress should display a menu with fewer options.

To implement this functionality on your WordPress website, all you need to do is to place the following code in custom_functions.php:

function custom_menu()
{
if( is_user_logged_in() )
{
$custom_menu = 'LoginMenu';
}
else
{
$custom_menu = 'VisitorMenu';
}
wp_nav_menu( array( 'menu' => $custom_menu, 'theme_location' => 'primary' ) );
}
add_action('wp_footer', 'custom_menu');

In the above code, we’ve used two separate WordPress menus — LoginMenu & VisitorMenu created using WordPress Menu Editor [Apperance > Menus]. After creating the menus you need to decide where you want the menus to be hooked. This code places the menu in footer area. You can however change its location by simply replacing the wp_footer in the code with the desired location.

Divi WordPress Theme