WordPress: How to share widget content within a custom sidebar across a Multisite

Question

I am creating a WordPress Multisite and I need a sidebar that will display on all the child websites but where the content will be added and changed from the parent main site. I have looked into plugin’s however none of been created specifically for widgets. I have seen a couple outdated posts where code has been written to add this functionality. However, I am unable to get it to work. I registered a sidebar

function wpb_widgets_init() {

  register_sidebar( array(
    'name' => __( 'SB Sidebar', 'pageside' ),
    'id' => 'sbsidebar',
    'description' => 'Appears in the sidebar on pages',
    'before_widget' => '<div class="pagesidebar">',
    'after_widget'  => '</div>',
  ) );

}

add_action( 'widgets_init', 'wpb_widgets_init' );

Then I added the code I found at https://redtreewebdesign.com/sharing-caring-wordpress-multisite/ to my functions.php on my child theme and changed the id name to match the id of my sidebar. However, the content added into the SB Sidebar on the main site is not being carried over to the child sites. Am I missing something? I have tried searching online for answers on if anyone was having similar issues with this code but unfortunately when it comes to Multi-sites there isn’t much information.

function example_multisite_sidebar( $sdsidebar ) {

  // Try to get the stored widget content for the sidebar.
  $markup = get_site_transient( ‘sidebar_cache_’ . $sdsidebar );

  // If we’re not on the main site and the transient exists, display the stored widget content.
  if (!is_main_site() && $markup ) {
    echo $markup;
  } else {
    // If we’re not on the main site and the transient doesn’t exist, we make a call to the main site, which kicks off example_multisite_sidebar_save().
    if (!is_main_site()) {
      $url = add_query_arg( array(‘get_sidebar’ => $sdsidebar), get_site_url( 1 ) );
      $request = file_get_contents( $url );
      echo $request;

      // display the content
      echo get_site_transient( ‘sidebar_cache_’ . $sdsidebar );
    } else {
      // If we’re on the main site and the transient doesn’t exist, store the widget content in the transient.
      ob_start();
      dynamic_sidebar($sdsidebar);
      $markup = ob_get_clean();
      set_site_transient( ‘sidebar_cache_’ . $sdsidebar, $markup, 4*60*60 );
      echo $markup;
    }
  }
}

add_action( ‘template_redirect’, ‘example_multisite_sidebar_save’ );
function example_multisite_sidebar_save() {

  // If we’re on the main site and the get_sidebar query var is set,
  // start a buffer that will record the widget content.
  if ( is_main_site() && isset( $_GET[‘get_sidebar’] ) ) {
    $sdsidebar = $_GET[‘get_sidebar’];
    ob_start();
    dynamic_sidebar( $sdsidebar );
    $markup = ob_get_clean();

    // Set a transient to store the HTML of the widget content
    set_site_transient( ‘sidebar_cache_’ . $sdsidebar, $markup, 4*60*60 );

    // Also display the content. This ensures that the content gets displayed if a site other than the main site is the first to be loaded after an update.
    echo $markup;

die();
  }
}

// Delete all sidebar transients when editing sidebar widgets.
add_action( ‘sidebar_admin_setup’, ‘example_multisite_sidebar_clear_cache’ );
function example_multisite_sidebar_clear_cache() {
  global $wp_registered_sidebars;
  foreach($wp_registered_sidebars as $sdsidebar){
    delete_site_transient(‘sidebar_cache_’ . $sdsidebar[‘id’]);
  }
}

0
, , , , Bethany 5 months 0 Answers 52 views 0

Leave an answer