Add a global menu to your network

Add a global menu to your network

About a year and a half ago I released the Network Wide Menu. Tonight, I made the network wide menu available on github.

When I released it there were quite a few people hoping that it added/output a new menu on the sub sites. I've written a bit of code that allows you to do that. What needs to be done to use the NWM for a new menu is

  1. Register a new menu across the network
  2. Make sure that menu is registered before the regular menus are registered on the sites. This ensures that the new global menu is the first menu slot on all sites.
  3. Hide the global menu admin on the sub sites so users do not attempt to populate it with items that will never be shown
  4. Add the menu to the themes across the network

You can accomplish the first 3 items with the following:

The best place to have that code is in a small plugin in the mu-plugins folder. The earlier this code executes the better.

Now all you need to do is add the menu output to your theme and style it appropriately in each theme:

  • Adam W. Warner
    Posted at 22:22h, 27 January Reply

    Glad to see you back here and posting Ron:)

  • Jason Green
    Posted at 02:05h, 03 February Reply

    Ron is back! woot! That’s for the post, informative. I will keep it in mind if I ever need a network wide menu. Keep being awesome Ron.

  • Caspar
    Posted at 08:12h, 21 March Reply

    Hi Ron,
    Does this code allow for translated menu items as well? I used your Network Wide Menu. Now that I’m translating my website I don’t find my subsite menu items being translated, only the main site.


    • Ron
      Posted at 08:32h, 21 March Reply

      How are you translating the menu items on your main site?

      • Caspar
        Posted at 10:29h, 21 March Reply

        They’re translated through qtranslate’s shortcodes [:en}translated text.

        • Ron
          Posted at 10:55h, 21 March Reply

          I’ve never used qtranslate but at a quess you would probably have to reproduce the translation settings on all the sub sites. NWM caches the menu items (the rows from the DB) not the rendered menu. Caching the rendered menu would only work if the same theme was used across the network.

          • Caspar
            Posted at 11:02h, 21 March

            OK I see. That would make NWM pretty useless for this purpose I guess. Thanks for clarifying this.

  • Todd
    Posted at 19:01h, 30 April Reply

    Hi Ron,

    I have this plugin active and working on a network of 125 sites and I have a problem. I need to deactivate it for a couple sites but don’t want to network deactivate and have to reactivate for all the sites that need it. Is there a way to just disable for a few blogs so they can have custom menus? Happy to compensate, please let me know when you can, thank you.

    • Ron
      Posted at 06:52h, 01 May Reply

      The easy way to do that on the sites where you do not want the global menu is register another menu (one that you’re not going to use) first before the theme menus are registered.

  • James
    Posted at 16:15h, 18 July Reply

    Hi Ron,

    Thanks for working on this plugin!

    I’m having difficulty getting your plugin working, Here’s what I’ve done,
    1) added your plugin .php file to my mu-plugins directory
    2) added your code from above (the first paste with register_nav_menu() and the admin_init) directly at the bottom of your plugins .php file in mu-plugins.
    3) added wp_nav_menu( ‘global’ ); to the header of each theme file

    And all I see is a big list of all the pages in my site?? What should I do?

    • Ron
      Posted at 19:51h, 18 July Reply

      You should register the menu in the functions.php in your themes instead of in the plugin. You will also need to style your menu in each theme.

      When you added it to header.php did you always place your code after the <body class=" ... ?

      Did you add menu items to the global menu in the main site?

      • James
        Posted at 22:22h, 19 July Reply

        Hi Ron, thanks for replying. Ok, I GOT IT! Finally.. Would you be willing to write out a detailed step-by-step on how to get this all working for other people? using a working example: “Do this in the Main Theme..” and so forth?

        I had a really hard time getting this working because you spoke about creating a menu using register_nav_menu() and showing it with wp_nav_menu() – but those function actually create and output menu locations not ‘menus’). After register the menu location I still had to create a menu in each theme and assign it to the menu location.

        Also, you never mentioned you have to actually VISIT the main website so the menu is output and ‘cached’ to the database by your plugin. By visiting the main site wp_nav_menu() in header.php runs and outputs the menu, that’s when the plugin actually caches the menu. Only then did the subsites began displaying the main site menu.

        Thanks though!

        • Ron
          Posted at 08:58h, 20 July Reply

          Also, you never mentioned you have to actually VISIT the main website so the menu is output and ‘cached’ to the database by your plugin.

          See the last point under Main Site Implementation –

          Would you be willing to write out a detailed step-by-step on how to get this all working for other people?

          The post that I linked to above explains everything you need to do if you want to use the first existing menu location in each theme. All of those instructions still apply when you create a new menu location for the global menu. The purpose of this post is only to explain how to create said new menu location.

  • Ben
    Posted at 06:22h, 01 August Reply

    Hi there,

    First: Thank you for your work, it is much appreciated. I am however a litte confused, how your plugin works (aka doesn’t work for me. 😉
    I wrote a theme with two menu locations (called ‘global’ and ‘local’).

    [snip to remove code pasted from post above]

    Your theme is active on all blogs in the network, I created two menus on every blog, one empty placeholder called “main menu” (which is only filled in the main blog), and one menu as local menu. I have put the menus in the correct locations via the theme settings.

    While it works on the main blog, it doesn’t work on other blogs: The global menu is shown in the location of the local menu which isn’t shown at all.

    I would be very grateful for a solution (doesn’t have to be flexible, I would even hammer ‘global’/’local’ in your plugins code if that helps).

    Thanks in advance,

    • Ron
      Posted at 09:25h, 01 August Reply

      You still need to implement the steps in

      • Ben
        Posted at 10:38h, 01 August Reply

        Thanks for your reply and your time, I am aware that I am asking for support on a free plugin, so it is very appreciated.

        [edit to remove paste of post I linked to in my previous comment]

        I have used the newest version of your plugin from GitHub, together with the code on this page. My WordPress-Version is 3.5.2.

        Do you have any idea, why the … the network-wide menu is shown in the wrong slot AND in the other slot no menu is shown … ?

        Thanks again,

        • Ron
          Posted at 10:54h, 01 August Reply

          The $slot = 1 attaches the network wide menu to the first menu registered in the theme functions file (vs the first one shown on the site). Changing it to to 0 (which I snipped out) would disable the plugin. Changing it to 2 would change it to the second registered menu. The slot applies to both the main site slot & the sub site slot.

          Keep in mind that the menu is cached. When you edit the code the cached copy of the menu will continue to be used. The cached menu is refreshed when you edit menus on the main site then view the front end of the main site.

          • Ben
            Posted at 11:15h, 01 August

            I see. I didn’t realize I had to edit menus on the main site, thought reloading the site was enough.

            I edited both my sub site and main site menus, making some small changes, reloaded the main site again, and now I have the network-wide menu in BOTH slots on the sub site (while two different menus are assigned to the two different slots in the theme settings). Main site is still working fine.

        • Ron
          Posted at 11:01h, 01 August Reply

          Ben, just out of curiosity – if your common theme already has two menus, why are you adding the global menu above?

          I wrote this tutorial for people who had different themes on each site.

          If you want to have a third menu, just add it to your theme (since you only have one theme to add it to), skip this tutorial and use the plugin per the original instructions for the plugin.

          • Ben
            Posted at 11:18h, 01 August

            My goal is that sub site admins can easily administrate “their” menu (which is placed in a different location than the main menu). The main menu which can only be administrated by the super-admin contains stuff like “About the network”, “Legal Info”, “Become a partner”, …

            The main menu also has to be easily administrated, because a non-techie is going to be the super-admin.

        • Ron
          Posted at 14:11h, 01 August Reply

          > My goal is that sub site admins can easily administrate “their” menu (which is placed in a different location than the main menu).

          Right, what I said in my previous comment still applies. Add a third menu to your theme & use the network wide menu plugin per it’s release post instructions.

          • Ben
            Posted at 20:55h, 01 August

            I don’t understand how a third menu would help, or what I would do with it. I only need two menus per blog (one administrated by a local administrator, one by a global administrator). I will investigate further tomorrow.

          • Ron
            Posted at 22:27h, 01 August

            @Ben – the title of the post is Add a global menu… If you already have enough menu locations then just use the network wide menu plugin (with your existing theme menu locations). You do not need to use the code I provided in the tutorial above.

          • Ben
            Posted at 05:35h, 02 August

            Hey Ron,

            I finally found a solution (was easier than I thought), see here:
            The relevant line is line 2. Basically it uses a certain hard-coded theme location (called ‘global’ instead of a slot). It works and I currently don’t see any drawbacks.

            Thank you again for your time, I will use the donate button now, have a beer/beverage of your choice on me. 🙂

Post A Comment