Skip to content

Instantly share code, notes, and snippets.

@bfintal
Last active July 13, 2016 05:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfintal/5b565eb32e8472e755a9 to your computer and use it in GitHub Desktop.
Save bfintal/5b565eb32e8472e755a9 to your computer and use it in GitHub Desktop.
Make `is_active_sidebar` work correctly with Custom Sidebars
/**
* This snippet will make `is_active_sidebar` work correctly with Custom Sidebars
* @author Benjamin Intal
*/
/**
* Gathers all the sidebar IDs and the replacement IDs. Most of this code froms from CustomSidebarsReplacer->replace_sidebars()
*/
add_action( 'cs_predetermineReplacements', 'verdant_custom_sidebars_determine_replacements' );
function verdant_custom_sidebars_determine_replacements( $defaults ) {
global $_verdant_sidebar_ids_to_replace;
$_verdant_sidebar_ids_to_replace = array();
$customSidebarReplacer = CustomSidebarsReplacer::instance();
$replacements = $customSidebarReplacer->determine_replacements( $defaults );
foreach ( $replacements as $sb_id => $replace_info ) {
if ( ! is_array( $replace_info ) || count( $replace_info ) < 3 ) {
continue;
}
// Fix rare message "illegal offset type in isset or empty"
$replacement = (string) @$replace_info[0];
$replacement_type = (string) @$replace_info[1];
$extra_index = (string) @$replace_info[2];
$check = $customSidebarReplacer->is_valid_replacement( $sb_id, $replacement, $replacement_type, $extra_index );
if ( $check ) {
$_verdant_sidebar_ids_to_replace[ $sb_id ] = $replacement;
}
}
}
/**
* Checks the sidebars being replaced and make corresponding is_active_sidebar calls to work
*/
add_filter( 'is_active_sidebar', 'verdant_custom_sidebars_is_active_sidebar', 10, 2 );
function verdant_custom_sidebars_is_active_sidebar( $is_active_sidebar, $index ) {
global $_verdant_sidebar_ids_to_replace;
if ( empty( $_verdant_sidebar_ids_to_replace ) ) {
return $is_active_sidebar;
}
if ( ! empty( $_verdant_sidebar_ids_to_replace[ $index ] ) ) {
// Return the current value if it's the same replacement
if ( $_verdant_sidebar_ids_to_replace[ $index ] == $index ) {
return $is_active_sidebar;
}
return is_active_sidebar( $_verdant_sidebar_ids_to_replace[ $index ] );
}
return $is_active_sidebar;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment