Find a widget’s current sidebar context

I recently was programming a widget for WordPress that needed to communicate with another widget if it was at a lower priority in the same sidebar. I came up with these nifty little functions to find the current context and sibling widgets of the current widget. You can add this to your functions.php.

function get_current_sidebar($widget_id) {

    $active_sidebars = wp_get_sidebars_widgets();
    $current_sidebar = false;
    foreach($active_sidebars as $key => $sidebar)
        if(array_search($widget_id, $sidebar))
            $current_sidebar = $key;    
    return $current_sidebar;

function get_current_sidebar_widgets($sidebar_id){

    $active_sidebars = wp_get_sidebars_widgets();
    $current_sidebar_widgets = $active_sidebars[$sidebar_id];

    return $current_sidebar_widgets;

Add this method to your widget object:

protected function has_widget_siblings(){

    $widget_id = $this->id_base.'-'.$this->number;
    $current_sidebar = get_current_sidebar($widget_id);
    $current_sidebar_widgets = get_current_sidebar_widgets($current_sidebar);
    $current_widget_identified = false;
    $current_widget_siblings = false;
    foreach($current_sidebar_widgets as $key => $widget){
        if(preg_match("/{$widget_id}/", $widget)){
            $current_widget_identified = true;
        elseif(preg_match("/{$this->id_base}/", $widget)){
                $current_widget_siblings = true;
                return $current_widget_siblings;

Read my post about passing data between classes to have your widget communicate with a sibling widget.