My WordPress Multisite Experience
Welcome to my first Multisite post! I certainly do not claim to be a WordPress Multisite expert (or even a WordPress expert), but I recently launched a Beta version of my first WPMU site.
As I write this, the Beta version of the site has 57 sites/users. The final site will have more than 3,700 sites/users, and new sites/users every day. So the last 2-3 months have been spent working on user permissions, the Theme Customization API, and an insane amount of conditionals, queries, and loops. (Not to mention the fact that I developed a search page that searches the options of each site, and I created a custom form to transfer the information from an old CodeIgniter database.)
But enough about this crazy project! I simply want to illustrate that I do have experience in some areas of Multisite. So now I will begin on my topic for this Monday…
Sharing a Sidebar Across a Network/Multisite
One of the most helpful functions to learn in WPMU is switch_to_blog(). If you’ve already searched for a solution for “sharing” a sidebar in WordPress Multisite, you probably found roughly a million ways to to the same thing (story of a developer’s life). I’m here to share my experience with you in the hopes that you won’t need to search quite as long or hard as I did.
My needs for sidebar sharing were as follows:
- All sites need to have the same sidebar.
- Sidebar content needs to be changed out by non-coders.
- Sidebar content can only be changed from one of the sites.
First, I tried using widgets for the sidebar. Then, I realized I had two (big) problems:
- When I duplicated a site, it didn’t copy the actual contents of the dynamic sidebar. Just the sidebar itself.
- When we would update the sidebar, we would have to update each site individually.
What I did in the end was create a plugin that I only activated on one site. I wrote a simple options page with the fields we needed. Then, in the sidebar.php template file, I used switch_to_blog() with the ID of the site that had the plugin activated. (Don’t forget to put restore_current_blog() after you get everything you need from the other site.) Finally, I echoed the options out between switch_to_blog() and restore_current_blog().
Essentially, what this did was allow sites 5, 6, 7, etc. to access the plugin options for site 1, and use that information in the sidebar.
I hope this information was helpful in understanding how to use switch_to_blog() with sidebars! Let me know if I should expand on this topic, cut it down, or if the amount of information was just right.
Tweet to @megabyterose