Loop through widgets in sidebar

This function from @birgire is almost perfect for me, but how do I call it using the sidebar array as the second parameter, rather than the widget ID. In his example, I would like to do something like this:

wpse_show_widget( 'lobby-sidebar', lobby-sidebar[0] );

In birgire's original post, he gets a Registered Widget called 'sidebar-1' and a registered widget with the ID 'calendar-2'.

Rather than trying to find the specific ID that WordPress assigns to a widget, I'd like the second parameter to be the array ID. That is far easier for me to find than the widget ID.

I modified birgire's function to use wp_get_sidebars_widgets();, but the codex says that's a private function not meant for theme development. I thought someone here might have a more elegant solution.

  • And what is lobby-sidebar[0]? Because it isn't correct PHP syntax, so it's a little bit hard to guess, what exactly are you trying to achieve... Nov 28, 2018 at 15:36
  • @KrzysiekDróżdż: sorry for the confusion. I added more explanation. I'll also add my modifications to the original post. I asked a new question rather than commenting on the original because my reputation score is very low.
    – red5
    Nov 29, 2018 at 14:35

1 Answer 1


I'm using this modification of the original post by birgire, but I don't know if it's "ok" to use wp_get_sidebars_widgets(); in this manner.

function wpse_show_widget_alt( $index, $id ) {
global $wp_registered_widgets, $wp_registered_sidebars;
    $did_one = FALSE;

    //This built-in func isn't meant for theme or plugin development. 
    //I don't know any other way to list widgets in an array sorted by sidebar.
$theWidgets = wp_get_sidebars_widgets();
$widgetNumber = $theWidgets[$index][$id];

// Check if $id is a registered widget
if( ! isset( $wp_registered_widgets[$widgetNumber] ) 
    || ! isset( $wp_registered_widgets[$widgetNumber]['params'][0] ) ) 
    return FALSE;

// Check if $index is a registered sidebar
$sidebars_widgets = wp_get_sidebars_widgets();
if ( empty( $wp_registered_sidebars[ $index ] ) 
    || empty( $sidebars_widgets[ $index ] ) 
    || ! is_array( $sidebars_widgets[ $index ] ) )
    return FALSE;

// Construct $params
$sidebar = $wp_registered_sidebars[$index];
$params = array_merge(
                //array( array_merge( $sidebar, array('widget_id' => $id, 'widget_name' => $wp_registered_widgets[$id]['name']) ) ),
                array( array_merge( $sidebar, array('widget_id' => $widgetNumber, 'widget_name' => $wp_registered_widgets[$widgetNumber]['name']) ) ),
                (array) $wp_registered_widgets[$widgetNumber]['params']

// Substitute HTML id and class attributes into before_widget
$classname_ = '';
foreach ( (array) $wp_registered_widgets[$widgetNumber]['classname'] as $cn )
    if ( is_string($cn) )
        $classname_ .= '_' . $cn;
    elseif ( is_object($cn) )
        $classname_ .= '_' . get_class($cn);
$classname_ = ltrim($classname_, '_');
$params[0]['before_widget'] = sprintf($params[0]['before_widget'], $widgetNumber, $classname_);         
$params = apply_filters( 'dynamic_sidebar_params', $params );

// Run the callback
$callback = $wp_registered_widgets[$widgetNumber]['callback'];            
if ( is_callable( $callback ) )
     call_user_func_array( $callback, $params );
     $did_one = TRUE;
return $did_one;}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.