W
WP Quick Search
Features Integration Pricing Documentation Blog Products Demo
Login Start for free
Login Start for free
Blog / WordPress/ WordPress Tutorial: Dynamically Switch Navigation Menu Based on User Login Status

WordPress Tutorial: Dynamically Switch Navigation Menu Based on User Login Status

2016-06-13 · Ryan · Post Comment

How to Display Different Menus Based on Login Status in WordPress

For WordPress sites that allow user registration (such as membership sites, communities, or multi-user blogs), a common requirement is to display different navigation menus based on whether a visitor is logged in. For example, logged-in users should see links like "User Center" or "Logout," while logged-out visitors should see links like "Login" or "Register."

The core approach is to create two separate menus and then use code logic to dynamically switch which menu is displayed based on the user's login status.

Step 1: Create Two Menus

First, create two menus in the WordPress admin.

  1. Go to Appearance > Menus.
  2. Create a new menu named "Logged-in Menu" and add links for logged-in users, such as a profile page, dashboard, or logout.
  3. Create another new menu named "Logged-out Menu" and add links for visitors, such as login, register, or about us.

Note: Record the exact names you assign to these menus (the "Menu Name"), as you'll need them in the code.

Step 2: Add the Function Code

Add the following code to the end of your current theme's functions.php file.

/**
 * Switch navigation menu based on user login status
 * @param array $args The arguments array for wp_nav_menu()
 * @return array Modified arguments array
 */
function wpdocs_switch_menu_based_on_login( $args ) {
    if ( is_user_logged_in() ) {
        // For logged-in users: show menu named "Logged-in Menu"
        $args['menu'] = 'Logged-in Menu';
    } else {
        // For logged-out visitors: show menu named "Logged-out Menu"
        $args['menu'] = 'Logged-out Menu';
    }
    return $args;
}
add_filter( 'wp_nav_menu_args', 'wpdocs_switch_menu_based_on_login' );

Code Explanation and Notes

  • Function Logic: The code uses is_user_logged_in() to check the user's status and modifies the menu parameter for wp_nav_menu() to specify which menu to display.
  • Critical Change: You must replace the menu names 'Logged-in Menu' and 'Logged-out Menu' in the code with the actual names you created in Step 1.
  • Menu Location: This code affects all menus called via wp_nav_menu() on the site. To target a specific menu location (like "Primary Menu"), add a check for $args['theme_location'] inside the condition.
  • Child Theme: To prevent code loss during theme updates, it's strongly recommended to make this modification in your child theme's functions.php file.

Advanced Usage and Alternatives

Beyond the core method, you can also consider:

  1. Using a Plugin: If you prefer not to edit code, search for and install plugins like "Conditional Menus" or "If Menu," which typically provide a visual interface to set menu display conditions.
  2. Conditional Menu Items: If only a few links need to be shown or hidden based on login status, consider using CSS classes or plugins like Nav Menu Roles to control the visibility of individual menu items.

After completing these steps, save the file and refresh your site. Your site will now automatically display the appropriate navigation menu based on the user's login status.

conditional displayFunctions.phpNavigation MenuTheme DevelopmentTutorialuser loginWordPress
Previous
No Plugin Needed: Add a Minimum Comment Length in WordPress
Next
Fix WordPress Scheduled Post Publishing Failures with Code

Post a Comment Cancel reply

Your email will not be published. Required fields are marked with *.

Quick Navigation
W
WP Quick Search
About Terms of Service Privacy Policy
© 2026 WP Quick Search Inc. All rights reserved. ·
17 0.033s 4.23MB

Notice