Created
February 17, 2018 10:51
-
-
Save hiranthi/d6497cca80849f2019b84cd03490347f to your computer and use it in GitHub Desktop.
Get current WP sidebar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ( ! function_exists( 'get_current_sidebar' ) ) | |
{ | |
function get_current_sidebar ( $widget_id ) | |
{ | |
$sidebars_widgets = get_option('sidebars_widgets', array()); | |
if ( is_array( $sidebars_widgets ) && isset( $sidebars_widgets['array_version'] ) ) | |
unset( $sidebars_widgets['array_version'] ); | |
$active_sidebars = apply_filters( 'sidebars_widgets', $sidebars_widgets ); | |
foreach ( $active_sidebars as $sidebar => $widgets ) | |
if ( count( $widgets ) >= 1 ) | |
if ( false !== array_search( $widget_id, $widgets ) && NULL !== array_search( $widget_id, $widgets ) ) | |
return $sidebar; | |
return false; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// inside your Widget Class | |
function widget ( $args, $instance ) | |
{ | |
... | |
// get the current sidebar's ID as used when registered, ie sidebar-default | |
$current_sidebar = get_current_sidebar( $this->id ); | |
// you could use the output in a switch, just using some dummy sidebar names here | |
switch ( $sidebar ) | |
{ | |
case 'sidebar-blog': | |
$widget_content = 'test'; | |
break; | |
case 'main-footer': | |
$widget_content = 'Other content'; | |
break; | |
default: | |
$widget_content = '<b>Default</b>'; | |
break; | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment