Sharing Widgets and Content Between WordPress Multisite Blogs
WordPress Multisite is great but sharing content between blogs is a bit of a pain. Right now the only solid option is the good old switch_to_blog() function. Research tells us that this is a memory-hungry function that isn’t suitable for larger, high-traffic sites and some say not suitable for the font-end at all. That being said, I’ve had a decent track record with it, the few times I’ve used it (when there was no other option). I ran into one of those “no other option” scenarios and after spending too many hours trying display one blog’s widgets on all my other multisite blogs, I had an epiphany (read: gave up) and decided that jQuery and its super simple ajax functions would do the trick. So here’s what I did. Place this in your theme file wherever you want to pull the content to…
<?php
global $blog_id;
$blog_id = get_current_blog_id();
// the id of the blog you want to pull from. if not that blog, run the script.
if($blog_id != '1'):
<script>
// Loading content from main blog…
jQuery(document).ready(function($) {
$('#loadingDiv').show();
$('#ajax-content').load('/path/to/main-page/ #footer-widgets', function() {
// where #ajax-content is the div to load data into and #footer-widgets is the selector for the div to pull data from
$('#loadingDiv').hide(); // hide our loading animation.
});
});
</script>
<div id="ajax-content">
<div id='loadingDiv' style="text-align: center; display: none;">
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/ajax-loading.gif" alt="Loading..." />
<br /><br />Loading Content…
</div><!-- ajax-content -->
<?php else: // we’re viewing the main blog (with an id of 1) so show the usual WP logic/content ?>
<?php // this is where the original content stays. In this case it's <div id="footer-widgets"> and all the logic to display widgets so that we have something to pull from on other blogs ?>
?>