Skip to content

Instantly share code, notes, and snippets.

@coreymcollins
Last active December 30, 2015 12:09
Show Gist options
  • Save coreymcollins/7826961 to your computer and use it in GitHub Desktop.
Save coreymcollins/7826961 to your computer and use it in GitHub Desktop.
Share Sidebars Across a Multisite Network
jQuery(document).ready(function($){
function wds_reset_footer_transient() {
// Run our AJAX call to delete our site transient
$.ajax({
type : 'post',
dataType : 'json',
url : ajaxurl,
data : {
'action' : 'wds-reset-transient',
'ei-widget-nonce' : wds_AJAX.wds_widget_nonce
},
error: function ( xhr, ajaxOptions, thrownError ) {
console.log( thrownError );
console.log( ajaxOptions );
}
});
}
// If one of our update buttons is clicked on a single widget
$( '.widgets-holder-wrap' ).on( 'click', '.widget-control-remove, .widget-control-close, .widget-control-save', function() {
// Get our parent, or sidebar, ID
var widget_parent_div = $(this).parents().eq(5).attr( 'id' );
// And if our parent div ID, or our sidebar ID, is one of the following
if ( widget_parent_div == 'sidebar-footer-1' || widget_parent_div == 'sidebar-footer-2' || widget_parent_div == 'sidebar-footer-3' ) {
// Run our function
wds_reset_footer_transient();
}
});
});
<?php
/**
* Enqueue and localize our scripts
*/
add_action( 'admin_enqueue_scripts', 'wds_enqueue_ajax_scripts' );
function wds_enqueue_ajax_scripts() {
global $current_screen;
// Only register these scripts if we're on the widgets page
if ( $current_screen->id == 'widgets' ) {
wp_enqueue_script( 'wds_ajax_scripts', get_stylesheet_directory_uri() . '/js/admin-widgets.js', array( 'jquery' ), 1, true );
wp_localize_script( 'wds_ajax_scripts', 'wds_AJAX', array( 'wds_widget_nonce' => wp_create_nonce( 'widget-update-nonce' ) ) );
}
}
/**
* Register our AJAX call
*/
add_action( 'wp_ajax_wds-reset-transient', 'wds_ajax_wds_reset_transient', 1 );
/**
* AJAX Helper to delete our transient when a widget is saved
*/
function wds_ajax_wds_reset_transient() {
// Delete our footer transient. This runs when a widget is saved or updated. Only do this if our nonce is passed.
if ( ! empty( $_REQUEST['ei-widget-nonce'] ) )
delete_site_transient( 'wds_footer_widgets' );
}
<?php
/**
* Generate sidebar for footer across all sites
*/
add_action( 'init', 'wds_footer_widgets_all_sites' );
function wds_footer_widgets_all_sites() {
if ( ! is_main_site() )
return;
if ( ! ( $wds_footer_widgets = get_site_transient( 'wds_footer_widgets' ) ) ) {
// start output buffer
ob_start();
// Display our footer sidebars ?>
<aside class="footer-widgets-1 widget-area col span_4">
<?php if ( ! dynamic_sidebar( 'sidebar-footer-1' ) ) : ?>
<?php endif; // end sidebar widget area ?>
</aside>
<aside class="footer-widgets-2 widget-area col span_4">
<?php if ( ! dynamic_sidebar( 'sidebar-footer-2' ) ) : ?>
<?php endif; // end sidebar widget area ?>
</aside>
<aside class="footer-widgets-3 widget-area col span_4">
<?php if ( ! dynamic_sidebar( 'sidebar-footer-3' ) ) : ?>
<?php endif; // end sidebar widget area ?>
</aside>
<?php // grab the data from the output buffer and put it in our variable
$wds_footer_widgets = ob_get_contents();
ob_end_clean();
// Put sidebar into a transient for 4 hours
set_site_transient( 'wds_footer_widgets', $wds_footer_widgets, 4*60*60 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment