In Multisite, can we reference widgets from our home site?
-
I have our home site at website.com with widgets in the footer. It’s a multisite with subdomains, so we have sites like sub.website.com. (I’m using a generic site name because we have multiple sites where this applies.)
We’d like to use the widgets in the footer on our main site in the footer of our subsites as well, but they don’t carry over. How could we do this?
My solution is to hard code the footer into the footer.php page in the template, but I’d like to keep it editable through the Widgets menu.
I’m aware of Multisite Widgets Context, which seems great, but doesn’t do what I need. I’d have to apply the widgets to all of my sites and have them reference the one on my main site. What I want is to copy the collection of widgets from the home site.
Right now, I say <?php the_widget( ‘footer’ ); ?> on my home site, which grabs its widgets from this collection and displays them. I would like to use this code on my subsites to get the same results, like <?php the_widget( ‘footer’, ‘home’); ?>
. That code won’t work, but you get the idea.Thanks!
- This topic was modified 5 years, 8 months ago by multimediocrity.
- This topic was modified 5 years, 8 months ago by multimediocrity.
-
PHP can only access data for the current site. Other site’s data is inaccessible. That said, we have the handy
switch_to_blog()
function allowing us to make any other site the current site. Once switched, we can access that site’s data. If such code is on a template file, any output still goes to the site for which the template belongs, only the data sourced belongs to the current site.Don’t forget to switch back after getting what you need from another site!
I found this article that could hold the answer: https://redtreewebdesign.com/sharing-caring-wordpress-multisite-sharing-widgets/
I also have widgets in the main site footer of a multisite that I thought I could use switch_to_blog() to access and display, but that function only works for menus. The article documents their process in which the widget data is stored in a transient, which is output on the subsite blog.
I am weighing my options between implementing this code versus using the switch_to_blog() function because my footer widgets are used just to display links. I could register menus for the footer instead so I can use the switch_to_blog() function. But if you want to have widgets in your subsites’ footers that reference the home site, you may need to use this code.
Update: Added the code from the article to my functions.php and it’s working. To implement in the footer.php file, I replaced my
<?php if ( is_active_sidebar( 'footer-right' ) ) { dynamic_sidebar( 'footer-right' ); } ?>
with
<?php example_multisite_sidebar( 'footer-right' ) ?>
Main site widgets are appearing in my subsites, and updating widget information on main site also updates subsites’ widget information. Next steps for me would be to put this code in mu-plugin and prefix/rename the function to fit my theme.
- The topic ‘In Multisite, can we reference widgets from our home site?’ is closed to new replies.