Skip to content

Instantly share code, notes, and snippets.

@taras
Created November 6, 2012 04:38
Show Gist options
  • Save taras/4022609 to your computer and use it in GitHub Desktop.
Save taras/4022609 to your computer and use it in GitHub Desktop.
My failed attempt at getting dynamic_sidebar() to work with switch_to_blog()
<?php
if ( !is_admin() ) {
add_filter('sidebars_widgets', 'ichannel_load_parent_widgets');
}
/**
* Filter loads sidebars from the parent site if current site's sidebar is empty.
* Also loads widgets from parent site into current site.
*
* @param $sidebar_widgets array
* @return array
*/
function ichannel_load_parent_widgets($sidebar_widgets){
global $wp_registered_widgets, $_wp_sidebars_widgets;
$parent_sidebars = get_blog_option(PARENT_SITE_ID, 'sidebars_widgets', array());
if ( $parent_sidebars ) {
foreach ( $parent_sidebars as $sidebar => $widgets ) {
# ignore wp_inactive_widgets because inactive widgets can't be displayed anyway
if ( array_key_exists($sidebar, $sidebar_widgets ) && $sidebar != 'wp_inactive_widgets' && empty($sidebar_widgets[$sidebar]) ) {
$sidebar_widgets[$sidebar] = $widgets;
if ( is_array( $widgets ) ) {
foreach ( $widgets as $widget ) {
# there might be a better way to do this, but I don't know what it is, so I'll brute force it
$pieces = explode('-', $widget);
if ( count($pieces) == 2 ) {
$option_name = 'widget_' . $pieces[0];
$id = $pieces[1];
$found_widgets = get_blog_option(PARENT_SITE_ID, $option_name);
if ( array_key_exists($id, $found_widgets) ) {
$wp_registered_widgets[$widget] = $found_widgets[$id];
}
}
}
}
}
}
}
return $sidebar_widgets;
}
function option_from_parent($option) {
add_filter("option_$option", 'load_option_from_parent_site', $priority = 10, $accepted_args = 2);
}
function load_option_from_parent_site( $value, $option ) {
return maybe_unserialize(get_blog_option(PARENT_SITE_ID, $option, array()));
}
option_from_parent('widget_widget_sp_image');
add_filter('option_sidebars_widgets', 'sidebar_widgets_get_from_parent');
function sidebar_widgets_get_from_parent($sidebar_widgets) {
return $sidebar_widgets;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment