Skip to content

Instantly share code, notes, and snippets.

@hiranthi
Created February 17, 2018 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiranthi/d6497cca80849f2019b84cd03490347f to your computer and use it in GitHub Desktop.
Save hiranthi/d6497cca80849f2019b84cd03490347f to your computer and use it in GitHub Desktop.
Get current WP sidebar
<?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;
}
}
<?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