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.
- Go to Appearance > Menus.
- Create a new menu named "Logged-in Menu" and add links for logged-in users, such as a profile page, dashboard, or logout.
- 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 themenuparameter forwp_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.phpfile.
Advanced Usage and Alternatives
Beyond the core method, you can also consider:
- 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.
- 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.