We’ve worked quite a bit with WordPress Multisite, the built-in functionality that allows you to create sub-sites off of your main website. These sub-sites are accessible and managed separately, and can either have their own theme or use the same theme as the primary site.
(There’s plenty we can talk about with the benefits and challenges of Multisite, but we’ll leave that for another post.)
One of the primary requests we get when building out a Multisite install is that there be consistent elements throughout the entire network of sites, such as the navigation and specific sidebar items. However, Multisite is really designed so that each sub-site is treated as its own site – meaning that the content from one site isn’t easily displayed on the other sites. Sometimes this makes a lot of sense; in other cases, it’s an obstacle that we need to overcome.
For example, we recently completed a Multisite project with five primary menu dropdowns – one for each of the five sub-sites in the network. The admins on each of the sub-sites control the content that goes into those menus, but it all needs to be displayed on every site (including the primary site).
To make that happen, we need to reach into the database and pull the content from the correct site. Enter switch_to_blog.
As described in the WordPress Codex:
Switch the current blog to a different blog. switch_to_blog(), is useful if you need to pull posts or other information from other blogs.
So if I’m looking at sub-site 3, but I need to display the content from a post in sub-site 1, switch_to_blog is the way to do it. Here’s an example of how it’s used.
switch_to_blog( $blog_id );
// Do something
You’ll notice a couple of things.
Here’s an actual example we’ve used; in this case, we’re pulling information from a custom field of a post in the primary site so that it can be displayed on the sidebar of any of the network sites.
<?php switch_to_blog(1); $meta_values = get_post_meta(4586, 'poll', true); echo $meta_values; restore_current_blog(); ?>
In addition to the example above, switch_to_blog is incredibly useful for making sure your navigation shows up correctly across every site. Here’s two examples of how that works.
If your menus are controlled only on the primary site, but need to be displayed on every network site:
<?php switch_to_blog(1); wp_nav_menu( array( 'container' => false, 'menu_id' => 'nav', 'theme_location' => 'primary' ) ); restore_current_blog();?>
If your menus are controlled individually by each site and need to be displayed across the entire network:
<?php switch_to_blog(2); wp_nav_menu( array( 'menu' => 'MENUTWO', 'items_wrap' => '<ul id="main-nav">%3$s</ul>', 'walker' => new themeslug_walker_nav_menu()) ); restore_current_blog(); switch_to_blog(3); wp_nav_menu( array( 'menu' => 'MENUTHREE', 'items_wrap' => '<ul id="main-nav">%3$s</ul>', 'walker' => new themeslug_walker_nav_menu()) ); restore_current_blog(); ?>
(There’s some additional Walker functionality in here that you should edit as appropriate.)
One caveat: switch_to_blog doesn’t work for everything. According to the codex, it doesn’t work for autoloaded option, plugins and the object cache. For the most part, however, it does everything you need to ensure that the content you create on one site of your network is accessible and viewable on every other site.