WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 4 years ago

#22370 closed defect (bug) (wontfix)

dynamic_sidebar does not consider switch_to_blog

Reported by: tarasm Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4.2
Component: Widgets Keywords:
Focuses: multisite Cc:

Description

On the main site of my WordPress network, I have a header that I would like to show on all other sites of the network. The main site uses the parent theme and all of the other sites use child themes. In header.php of the parent theme, I'm using switch_to_blog function to load Navigation Menus from the main site in the header of the other sites. This is working perfectly.

The Problem

I also have a sidebar in the header that I would like to manage on the main site as I do with the navigation menu. Unfortunately, this doesn't work at all because dynamic_sidebar function does not take into consideration switch_to_blog. I get no output instead of showing sidebar from main site.

Others with same problem

Boone Gorges did a good job of explaining this problem on WordPress Stackexchange on the question titled: Sharing Dynamic Sidebars across Multiple Blogs.

Change History (6)

#1 @jeremyfelt
5 years ago

  • Component changed from Multisite to Template
  • Focuses multisite added

#2 @nacin
5 years ago

  • Component changed from Template to Widgets
  • Focuses template added

I'm not sure I see why this wouldn't work, unless the widgets (the PHP) in use weren't included on the sub-sites.

#3 @clifgriffin
5 years ago

It appears that when switching context, it's not enough to have active sidebars in the active context. They actually have to be active in the current blog.

I hope that makes sense.

#4 @westonruter
5 years ago

Yeah, the problem is that all widgets instances are registered during widgets_init up front when WordPress is initializing, so doing switch_to_blog is going to indeed switch the sidebars_widgets but then it is going to be referring to widget IDs that are not registered. I suppose you could try re-running do_action( 'widgets_init' ) after you do your switch_to_blog() call, but no guarantees. There's also the problem of needing to re-initialize $wp_reigstered_sidebars; see http://wordpress.stackexchange.com/a/48227/8521

An alternative solution was written up by WebDevStudios, which involves using Ajax to fetch the widgets from other blogs, though the widgets could be fetched server-side via wp_remote_get() too of course: http://webdevstudios.com/2013/12/17/sharing-sidebars-across-a-multisite-network/

#5 @obenland
4 years ago

  • Focuses template removed

#6 @jeremyfelt
4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Similar to several other things in core, switch_to_blog() changes context in a way that ignores the underlying code needed to register sidebars, widgets, etc. so that they are available in the current context.

I'm going to close this as wontfix, only because it's somewhat expected behavior.

Note: See TracTickets for help on using tickets.