April 16, 2014
by Jason Roman





Categories:
Protips

Tags:
PHP  WordPress


How to Incorporate WordPress Network-Wide Menus

Use a plugin to reuse multiple menus across your WordPress network.

I recently added a repository for a WordPress plugin that allows you to reuse the same menu or set of menus across your WordPress network (multi-site). It created the plugin for a site that had a subdomain with the same look and theme as the main site, and contained multiple menus.


Installation

The basic usage is that you install the plugin to your wp-plugins directory and then enable it across the network. This is a network-wide plugin, but if you only want to activate it on specific sub-sites, remove the Network: true line in the header of the plugin file.

Main Site

  • Create your network menu in the main site
  • Assign the menu to the specified menu slot
  • Populate the menu in the main site
  • Load the main site to initiate the cache saving of the menu

Sub-Sites

The sub-sites must be using a theme that registers WP nav menus and contain at least one menu with one menu item. You simply repeat this process for the remaining menus, making sure they are all assigned to the corresponding menu slots from the main site.

Customizing

By default the plugin uses (up to) the first 10 menus of the main site. In order to change that, you must modify the $network_menu_indices variable. For example:
// use the first 3 menus instead of the first 10
$network_menu_indices = range( 0, 2 );

// or, use the first, third, and fourth menus
$network_menu_indices = array( 0, 2, 3 );
If for some reason you find yourself using more than 10 menus, you would have to modify the indices as well as the for() loop in the menu flush:
// use the first 15 menus
$network_menu_indices = range( 0, 14 );
...

// then in jayro_wp_nav_menu_flush()
for ( $i = 0; $i < 15; $i++ ) {
...
}
Note that every time you update the main menu, you may need to reload the main site to regenerate the menu cache. If you have any questions or need any help with the plugin, please respond in the comments.

This is forked from the original network-wide-menu plugin. To see a blog post about incorporating that plugin into your site, click here.


comments powered by Disqus