WordPress Buddypress Different Menu for Logged In and Non-Logged in Users

By March 13, 2012Web Design

I’ve been in the process of building an online community for the Utah Grotto of the National Speleological Society and ran into a wall when it comes to creating a different menu for logged in and non logged in users. The primary problem was in the fact that several of the buddypress supported themes only support one menu and you need at least two in order for the is_user_logged_in() function to work properly. So here was the solution I came up with after referencing the following sites: http://wpsnipp.com/index.php/theme/display-different-menus-for-logged-in-users/ and http://smartcoderszone.com/2010/07/how-to-add-multiple-menu-in-wordpress/.

  1. In the functions.php file of the theme in which you are using add  the following code in order to support two different menus that you define in Appearance > Menus within WordPress
    // This theme uses wp_nav_menu() in one location.
    	register_nav_menus( array(
    		'primary' => __( 'Primary Navigation', 'buddypress' ),
    	) );
    // I added this line to support a second menu for logged out users
    	register_nav_menus( array(
    		'secondary' => __( 'Secondary Navigation', 'buddypress' ),
    	) );
  2. Now log into WordPress and go to Appearance > Menus and create two different menus, one for logged in users and one for logged out users. For more information on using Menus go to http://codex.wordpress.org/Appearance_Menus_Screen.
    Wordpress Menus Screenshot

    After logging into your wordpress site go to the “Appearance” menu item on the left and then to “Menus”

  3. Finally add the following code to your header.php file in your theme folder, wherever the menu code appears. I swapped this line of code:
    <?php wp_nav_menu( array( 'container' => false, 'menu_id' => 'nav', 'theme_location' => 'primary', 'fallback_cb' => 'bp_dtheme_main_nav' ) ); ?>

    for this code:

    <?php
    if ( is_user_logged_in() ) {
        wp_nav_menu( array( 'container' => false, 'menu_id' => 'nav', 'theme_location' => 'primary', 'fallback_cb' => 'bp_dtheme_main_nav' ) );
    }
    else {
        wp_nav_menu( array( 'container' => false, 'menu_id' => 'nav', 'theme_location' => 'secondary', 'fallback_cb' => 'bp_dtheme_main_nav' ) );
    }
    ?>
  4. That’s it.

2 Comments

  • MK says:

    This worked really well – thank you so very much for posting. One thing I’d like to mention – the pages associated with the member only menu and the pages associated with the non-member menu should be completely different – otherwise, the menus start switching back and forth.

Leave a Reply