Show primary blog navigation for users - multisite / network

I am looking to create a function that will show a user their primary blog navigation instead of sites default navigation when logged in. So let's say a user is logged on and is a member of site 1(home, all users are members) and 5. If they are on site 1-5 they should see the navigation that was generated for site 5, even on the home site. If they are logged out, each sites navigation should function regularly.

I've created some base code, but am a little confused on passing nav from other sites across to already registered navigation.

function user_specific_nav() {
    global $user;
    $primary_blog = get_user_meta( $user->ID, 'primary_blog', true );

    if(is_user_logged_in() ) {
         wp_nav_menu(
                array(
                'menu' => 'USER PRIMARY SITE NAV',
                'theme_location' => 'primary',
                'fallback_cb' => false
              )
        );
     } else {
         wp_nav_menu(
                array(
                'menu' => 'DEFUALT SITE NAV',
                'theme_location' => 'primary',
                'fallback_cb' => false
              )
        );
     };
}

I've grabbed the users primary blog id with the first part of the function, but need to pass it into the wp_nav_menu.

Thank you.

  • Hoang Ngo

    Hi there,

    I hope you are well today.
    Can you please check this code?

    function user_specific_nav() {
    	global $user;
    	$primary_blog = get_user_meta( $user->ID, 'primary_blog', true );
    
    	if ( is_user_logged_in() ) {
    		//get users blog
    		$user_blogs      = get_blogs_of_user( get_current_user_id() );
    		$current_blog_id = get_current_blog_id();
    		foreach ( $user_blogs as $key => $blog ) {
    			//make sure not the primary blog
    			if ( $blog->userblog_id != 1 ) {
    				switch_to_blog( $blog->userblog_id );
    				wp_nav_menu(
    					array(
    						'menu'           => 'USER PRIMARY SITE NAV',
    						'theme_location' => 'primary',
    						'fallback_cb'    => false
    					)
    				);
    			}
    		}
    		//revert the current blog
    		switch_to_blog( $current_blog_id );
    	} else {
    		wp_nav_menu(
    			array(
    				'menu'           => 'DEFUALT SITE NAV',
    				'theme_location' => 'primary',
    				'fallback_cb'    => false
    			)
    		);
    	};
    }

    I have left some comments on the code :slight_smile:. If you have any additional issues, please let us know and we'll be happy to help.

    Best regards,
    Hoang

    • brettlewis

      I think there are a few issues with this. Is this a function you add to wp_nav_menu_item filter? It also uses a $primary_blog variable to get a specific key from the user meta data, but it doesn't retrieve the ID, or use it in foreach at all.

      If I'm reading this right, the fucntion should:
      - Check the users primary blog id.
      - IF they are on blog 1, change the primary menu to that of their primary blog. (same theme location, same menu name)

      wp_nav_menu(
                          array(
                              'menu'           => 'primary',
                              'theme_location' => 'top-bar',
                              'fallback_cb'    => false
                          )
                      );

      - ELSE IF they are on their primary blog, do nothing.

      Switch to blog is what I thought it needed to use, but it doesn't seem to be passing the users primary blog through correctly.

      Thanks for the help!

  • brettlewis

    I went ahead and changed the entire methodology used once I read more into "switch blog" function.

    I've created a solution by declaring a general blog switching function like so:

    function blog_switch() {
        if ( is_user_logged_in() ) {
        $user_ID = get_current_user_id();
        $primary_blog = get_user_meta( $user_ID, 'primary_blog', true );
        switch_to_blog( $primary_blog  );
        }
    }

    And then whereever I need to use a componenet from the users primary blog I wrap the blog switch function around the content like so:

    <?php
    // Apply user primary blog id
    blog_switch();
    //Load menu
    foundationpress_top_bar_r()
    //Restore current blog
    php restore_current_blog();
    ?>

    This solution works, but I'd still be curious if you think this solution is the best. Are there any issues using it like this? I've read that "switch_to_blog" is a resource hog, but there doesn't seem to be many other viable options.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.