How to Show Different Menus to Logged in WordPress Users

Published On: April 10th, 2016|Last Updated: January 7th, 2023|Categories: WordPress|Total Views: 1920|Daily Views: 1|

In WordPress, there is an option to create multiple menus but no condition to make it visible only for logged in users or guest. Users might need this option to show different menus to logged in WordPress users for various scenarios like “login menu and guest post items should visible only for guest” and “logout button should visible only for logged in users”.

See Also: 10+ Most Common WordPress Mistakes to Avoid

Create Two Menus – (if not created it already)

  1. Login to WordPress admin dashboard.
  2. Navigate to Appearance Then Menus.
  3. Click create new menu.
  4. Create two menus. I have created two menus with name guest and logged-in.

Show Different menus to Logged in WordPress Users

See Also: Top 16 Best Free Essential WordPress Plugins

Using code – Show Different Menus to Logged In WordPress Users

Add the below lines to the functions.php file in the theme folder.

// Show different menus to logged in users and guest
function ett_diff_menu( $args = '') {
    if( is_user_logged_in()) {
         $args = 'logged-in';
      }
    else {
         $args
['menu'] = 'guest'; } return $args; } add_filter( 'wp_nav_menu_args', 'ett_diff_menu' );

Modify logged-in and guest with your menu name. Save the file. Now you can see logged-in menu will appear only to logged in users and guest menu will appear only to guests.

See Also: How to make WordPress site Load Faster

If your theme has eligible to display more than one menu

The above method will work only if your theme has one menu location. If it has more than one menu to display like header menu, main menu, footer menu, social menu etc, then the above method will display the same logged-in and guest menus in all the menu location overriding the settings in Menus panel. So apply the below method if your theme contains more than one menu location.

Add the below lines to the functions.php file in the theme folder.

// Show different menus to logged in users and guest
function ett_diff_menu( $args = '') {
     if ($args['theme_location'] == 'header-menu') {
         if( is_user_logged_in()) {
              $args['menu'] = 'category';
           }
         else {
              $args['menu'] = 'category-join';
           }
         }
return $args;
}

add_filter( 'wp_nav_menu_args', 'ett_diff_menu' );

Modify the header-menu to your menu location name. You can find your menu location name in the theme’s header file or from your theme provider. Modify logged-in and guest with your menu name. Save the file. Now you can see the logged-in menu and guest menu will appear only in the header menu and remaining menu locations will be unaffected.

See Also: Prevent WordPress Content Theft – text and images

Using Plugin – Show Different Menus to Logged In WordPress Users

Above methods are applicable only to advanced users. Basic users can easily accomplish the same using any of the below plugin instead of editing the functions.php file.

  1. Nav Menu Roles
  2. If Menu
  3. Menu Item Visibility Control

See Also: Top 3 Best Free WordPress Security Plugins

3 Comments

  1. Avatar of Jayavinoth
    Jayavinoth Sep 20, 2016 at 10:07 am - Reply

    helpful…

  2. Avatar of Gypsy Mathew
    Gypsy Mathew Aug 18, 2016 at 11:01 pm - Reply

    Very clear instruction which I exactly need. I done using the 1st method. Great man, Keep going….

  3. Avatar of Shali
    Shali Apr 14, 2016 at 5:28 pm - Reply

    works perfectly. thanks

Leave A Comment