Instantly share code, notes, and snippets.

Embed
What would you like to do?
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