Creating a Conditional Main Menu for Members – New Nav Menu Add On

We’ve blogged previously about using the Nav Menu Roles plugin along with the PMPro Roles add on to create conditional menu items. Here’s another tutorial that demonstrates two options for conditional navigations menus: editing your theme or our super cool new Nav Menus Add On.


Option 1: Edit Your Theme

This demo is particularly useful if you’re a theme author looking to build member menus directly into your theme.

Step 1: Register the Members Navigation Menu

In your theme’s functions.php file, add the following code:

function mytheme_register_my_members_menu() {
	register_nav_menu( 'primary-members', __( 'Primary Menu - Members', 'theme-slug' ) );
	register_nav_menu( 'primary-nonmembers', __( 'Primary Menu - Non-Members', 'theme-slug' ) );
}
add_action( 'after_setup_theme', 'mytheme_register_my_members_menu' );

Duplicate the ‘register_nav_menu’ line for every new menu needed, such as a conditional footer menu, or for each membership level on your site to create a unique menu by level ID.


Step 2: Edit Your Theme’s header.php File

The code below should replace your theme’s ‘wp_nav_menu’ code. If there are special attributes declared in your theme’s existing ‘wp_nav_menu’ code, you should mirror those for the new member/non-member menus. See this guide from the WordPress Codex for help.

//is there a user to check?
if ( is_user_logged_in() && function_exists('pmpro_hasMembershipLevel'))
{
	//check level
	if(pmpro_hasMembershipLevel())
		wp_nav_menu( array( 'theme_location' => 'primary-members' ) );
	else
		wp_nav_menu( array( 'theme_location' => 'primary-nonmembers' ) );
}
else
{
	wp_nav_menu( array( 'theme_location' => 'primary-nonmembers' ) );
}

Step 3: Create the Members and Non-Members Custom Menus

pmpro-members-menu

  1. In the WordPress admin go to Appearance > Menus.
  2. Click the “create a new menu” link to add a new “Members” menu.
  3. Add menu items to this menu, check the “Primary Menu – Members” Theme Location under “Menu Settings”, and save the menu.
  4. Click the “create a new menu” link to add a new “Non Members” menu.
  5. Add menu items to this menu, check the “Primary Menu – Non-Members” Theme Location under “Menu Settings”, and save the menu.

You’re Done!

From here you should navigate to your site both as a member and a non-member to verify that everything is working as intended.


Option 2: Use the Nav Menus Add On

We just released a new add on that does all of this for you. The Nav Menus Add On creates member navigation menus and swaps your theme’s navigation based on a user’s Membership Level.

This plugin duplicates all wp_nav_menus defined by your theme and creates a members version. Customize menus even further by optionally creating a level-specific navigation menu. This can be set on the Edit Membership Level screen under the “Navigation Menu” section.

View and Download the Add On