How to Conditionally Show Different Menus to Logged In Users in WordPress

How to Conditionally Show Different Menus to Logged In Users in WordPress

Are you running a WordPress membership website? Do you need to show different menu options to different groups of people – like, one menu for users who are not logged in to your website and a different menu to users that are logged in? In this article, you'll learn how to conditionally show different menus to logged in users in WordPress.

What's covered here:

  • How to show different WP menus to logged in users
  • How to show different WP menus for specific menu locations
  • How to show different WP menus for different user roles

First, we'll go through it programmatically and then I'll share a WP plugin that lets you conditionally show menus, too.

Programmatically Displaying Different WP Menus

It's surprisingly simple to programmatically display different WordPress menus. Like most things WordPress, you can do it easily by adding a short piece of code to your theme's functions.php file. This method works even if your theme only uses a single menu position.

But, first thing's first – create the different menus.

From the WordPress Dashboard, go to Appearance → Menus and create the menus. For the sake of this example, let's name the menus “Logged In” and “Logged Out”.

WP Menus for Logged In and Logged Out Users

Now that you have created two different menus, it's time to add the conditional code.
 

Copy and paste the following snippet in to your theme's functions.php file.

/**
* Set up conditional WordPress menus for logged in users
* @args
*/
function my_custom_menus( $args = '' ) {


	// Check if the user is logged in
	if( is_user_logged_in() ) { 
		
		// The user IS logged in.
		// Show the Logged In menu
    	$args['menu'] = 'logged-in';
    	
	}
	else {
	
		// The user IS NOT logged in.
		// Show the Logged Out menu
    	$args['menu'] = 'logged-out';
    	
	} 

   	return $args;

}

add_filter( 'wp_nav_menu_args', 'my_custom_menus' );

That's all the code you need to conditionally swap WordPress menus for users that are logged in or logged out.

Different Menus for Specific Menu Locations

it is possible to conditionally target specific menu positions. This takes the above example a step further and is useful when you're working with a WP theme that takes advantage of multiple menu locations (such as header and footer menus – or primary and social media navigation menus).

/**
* Set up conditional WordPress menus for logged in users
* @args
*/
function my_custom_menus( $args = '' ) {


	// Check if the menu's theme location is the "primary menu"
	if( $args['theme_location'] == 'primary-menu' ) { 

		// Check if the user is logged in
		if( is_user_logged_in() ) {
			
			// The user IS logged in.
			// Show the Logged In menu, as the primary menu.
    		$args['menu'] = 'logged-in';
    		
    	}
    	else {
    	
    		// The user IS NOT logged in.
    		// Show the Logged Out menu, as the primary menu.
    		$args['menu'] = 'logged-out';
    		
    	}
    	
	}

   	return $args;

}

add_filter( 'wp_nav_menu_args', 'my_custom_menus' );

Different Menus for Different WP User Roles

It's also possible to display different menus for different WordPress user roles by mixing the current_user_can() function into the conditional IF statements.

Here is an example of how to conditionally show different WP menus based on the current user's role capabilities:

/**
* Set up conditional WordPress menus for logged in users
* @args
*/
function my_custom_menus( $args = '' ) {


	// Check if the current user is an administrator
	if( current_user_can('administrator') ) { 

    	$args['menu'] = 'logged-in';
    	
	}
	else {
	
		$args['menu'] = 'logged-out';

	}

   	return $args;

}

add_filter( 'wp_nav_menu_args', 'my_custom_menus' );

Checking if the current user is a specific user rule makes the assumption that the user is also currently logged in. 

It's possible to mix and match any conditions that suit your needs. For instance, you could check that the user is an administrator AND that the menu location you're manipulating is the primary menu using if( $args['theme_location'] == 'primary-menu' && current_user_can('administrator') ) { … } .

Displaying Different WordPress Menus Using a Plugin

It's no surprise that there are also free WordPress plugins that handle conditionally displaying WordPress menus, too. Some plugins allow you to conditionally show individual menu items and others will swap the entire menu. WordPress has been around long enough that almost everything you need to do has been modularized and turned into a plugin.

Conditionally Showing Individual WP Menu Items

User Menus is a WordPress plugin by Jungle Plugins for showing different individual WP menus items depending on the current user. It can be set up to display different menu items if the user is logged in or logged out, based on the user's role, or based on information from the user's profile.

Conditionally Showing Different WP Menus

Conditional Menus is a WordPress plugin by ThemifyMe that allows you to establish rules for conditionally showing different WP menus. You can set it up with single conditions or combine conditions to create advanced rule settings. It's simple, but also quite robust.