Networkwide Menu

Since WordPress 3.0 was released we’ve had quite a few inquiries about the possibility of having the same menu across a network. This morning I had a bit of time to pull together a small plugin that accomplishes that. Basically what it does is cache a menu on the main site and display it on the subsites.

Sub Site Requirements

Each of the sub sites must:

  • be using a theme that registers WP nav menus.
  • have at least one menu with one or more menu items. The wp_nav_menu() function exits early without displaying a menu if it does not find a menu with menu items.
  • have a nav menu assigned to the specified slot. It can be an empty menu. The reason for this is the same as the previous limitation.

Main Site Implementation

  • Create your network menu in the main site.
  • Assign it to the chosen menu slot.
  • Populate your chosen menu on the main site.
  • Load the front end of the main site.

The reason for the last step is that each time you change a menu item on the main site a flag is set to flush the cached menu. The cached menu is refreshed the next time the menu is generated on the main site. Download: Networkwide Menu (4048)

Customizing for your use

  • Network wide – I made it a network wide plugin. If you want to activate it only on specific subsites, then remove the Network: true line in the plugin header.
  • Menu slot – the first line of code below the copyright notice is $slot = 1;. The $slot variable is used to determine which menu slot to use when replacing the menu. As far as this plugin goes, menu slots are determined by the order in which they are registered by the theme. The first menu location registered is slot 1. The second menu registered is slot 2 and so on.

Comments

  1. I have tried to activate this on my site but i cannot get it to work.
    I simply removed the network true to activate it on one subsite and activated the plugin.
    IN teh theme there is a call to wp_nav_menu() as normal.

    Do i need to at leasat create the menu in the subsite with the same name?

    • For obvious reasons, the plugin needs to be active on the main site. Otherwise no menu will ever be cached.

      Also, in the post I said

      Each of the sub sites must have at least one menu with one or more menu items. The wp_nav_menu() function exits early without displaying a menu if it does not find a menu with menu items.

      • I was able to get this to work but not with widgets. I don’t quite get it.

        • It doesn’t work with widgets. It only works with theme locations registered by themes.

          • Any suggestions on how to get it to work as a widget or is that something you might try to add some time?
            Thanks

          • Trying to do that with a widget would be difficult because widgets are multi-use & the settings are stored by widget ID which is generated by WP when you add the widget to a sidebar.

  2. Erlend Sogge Heggen says:

    I would love to test the plugin, but I don’t know what to expect of it, nor can I figure out whether or not I’ve fulfilled the criteria in order for it to be working properly.

    I have it installed on a WAMP multisite setup. All my sites have been set up with a native WordPress menu, including the main blog.

    - Where should I expect the networkwide menu to show up? In the Theme Locations widget, above/below Primary Menu somewhere?
    - I don’t get what you’re referring to with the steps “Assign it to the chosen menu slot.” and “Populate your chosen menu on the main site.”
    Will the plugin fail everywhere if one of my subsites is missing a native menu, or will it fail to load just on that theme/site?

    • Last question first – it’s per site. The only one that needs to have a menu is the main site because that’s where it gets cached from.

      You have to be using themes that add theme support for menus, register at least one native menu and displays the menu on the front end by calling wp_nav_menu(). You only get the menu slot (Menu Location) in the menu screen if the add_theme_support is called by the theme.

  3. Erlend Sogge Heggen says:

    Okay, thanks. So I’m using Twenty Eleven on my main blog. Here’s my setup:
    http://erlendsh.imgur.com/wpmu_networkwide_menu#m4Hgg

    Still having trouble getting to that last step.

  4. So, can we have more than one network-wide menu?

  5. Did anyone get this working? I have a variation of a slightly older 2010/11 template and have the plugin installed network wide, its just not recognizing it on my multisites.

    Thanks

    • Per the post the limitations are

      Each of the subsites must be using a theme that registers WP nav menus.

      Each of the sub sites must have at least one menu with one or more menu items. The wp_nav_menu() function exits early without displaying a menu if it does not find a menu with menu items.

      Each of the subsites must have a nav menu assigned to the specified slot. It can be an empty menu. The reason for this is the same as the previous limitation.

  6. Hi Ron,

    This looks like a great plugin! Can you please explain, step-by-step how to get this plugin working? Specifically, how to implement it?

    I’ve followed the ‘Implementation’ steps at the top, though I’m unsure how to actually display the menu on the subsites in the site network. Each subsite has a menu of it’s own, so menus are enabled. Lastly, each subsite is a child-theme, would that have any implication?

    Thanks, Ron!

    James

    • Can you please explain, step-by-step how to get this plugin working?

      The step by step instructions are in the post.

      I’m unsure how to actually display the menu on the subsites in the site network

      You don’t need to do anything other than ensure the sub site limitations are met on each site. If you have done those three things on the sub site & followed the implementation steps, the menu will automatically replace the selected menu on the sub site.

      • Hi Ron,

        Ok, can you please be more specific? I’ve followed your 4 steps, but there is nothing ‘automatically replaced’. Using wp_nav_menu() on the subsites shows only an empty menu. Just asking for detailed instructions because these 4 steps don’t explain anything about a menu being replaced or how to display the primary menu on the subsites. Do all the menus on each subsite need to have the same ‘slot’? Can you explain how to make sure they are in the same ‘slot’?

        Create your network menu in the main site. – Done
        Assign it to the chosen menu slot. – Done
        Populate your chosen menu on the main site. -Done
        Load the front end of the main site. -Done

        Now what?

        Thank you.

        • Did you do these per my previous comment?

          Each of the subsites must be using a theme that registers WP nav menus.

          Each of the sub sites must have at least one menu with one or more menu items.

          Each of the subsites must have a nav menu assigned to the specified slot. It can be an empty menu.

          Can you explain how to make sure they are in the same ‘slot’?

          As far as this plugin goes, menu slots are determined by the order in which they are registered by the theme. The first menu location registered is slot 1. The second menu registered is slot 2 and so on.

          Those are usually registered in the theme functions file

  7. For the first time on this site, I’m closing comments on a post. This plugin consists of 25 lines of code. If each line of code took 1 minute to write then the whole plugin would be 25 minutes. The actual time to write it was less than 15 minutes.

    This plugin does not add a new menu to the sub site. If you have followed the instructions I have provided, look at the existing menus displayed on your sub site. If necessary, hover over the menu links to see that the menu from the main site is populating the menu on the sub site.

    Menu location registration is done in the theme functions file. If you want to know what order your menus are registered in, that’s the file you need to look in for each of your themes.