Loop through widgets in sidebar

I have a sidebar called footer, I know I can use dynamic_sidebar() but I need to be able to split that into individual widgets.

I have the following so far which gives me the widget name e.g. text-1, category-2 etc but I am after the content for each of those.

Read More
$widget_list= get_option('sidebars_widgets');
if($widget_list)
 foreach($widget_list["footer"] as $widget){
  var_dump($widget);
 }

Is this possible to get the widget content from the widget name?

Related posts

1 comment

  1. The registered widgets in your sidebar-1 might be

    [sidebar-1] => Array
        (
            [0] => categories-2
            [1] => archives-4
            [2] => recent-comments-4
            [3] => calendar-2
            [4] => search-2
            [5] => archives-2
            [6] => text-4
            [7] => recent-posts-2
            [8] => nav_menu-2
        )
    

    If you got for example the calendar-2 widget, then from:

    print_r( $GLOBALS['wp_registered_widgets']['calendar-2'] );
    

    you will get something like:

    [calendar-2] => Array
        (
            [name] => Dagatal
            [id] => calendar-2
            [callback] => Array
                (
                    [0] => WP_Widget_Calendar Object
                        (
                            [id_base] => calendar
                            [name] => Calendar
                            [widget_options] => Array
                                (
                                    [classname] => widget_calendar
                                    [description] => A calendar of your site’s Posts.
                                )
    
                            [control_options] => Array
                                (
                                    [id_base] => calendar
                                )
    
                            [number] => 2
                            [id] => calendar-2
                            [updated] => 
                            [option_name] => widget_calendar
                        )
    
                    [1] => display_callback
                )
    
            [params] => Array
                (
                    [0] => Array
                        (
                            [number] => 2
                        )
    
                )
    
            [classname] => widget_calendar
            [description] => A calendar of your site’s Posts.
        )
    

    You can therefore try this modified version of dynamic_sidebar() to call a widget that you have placed in a given sidebar:

    /**
     * Show a given widget based on it's id and it's sidebar index
     *
     * Example: wpse_show_widget( 'sidebar-1', 'calendar-2' ) 
     *
     * @param string $index. Index of the sidebar where the widget is placed in.
     * @param string $id. Id of the widget.
     * @return boolean. TRUE if the widget was found and called, else FALSE.
     */
    function wpse_show_widget( $index, $id )
    {
        global $wp_registered_widgets, $wp_registered_sidebars;
        $did_one = FALSE;
    
        // Check if $id is a registered widget
        if( ! isset( $wp_registered_widgets[$id] ) 
            || ! isset( $wp_registered_widgets[$id]['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) $wp_registered_widgets[$id]['params']
                  );
    
        // Substitute HTML id and class attributes into before_widget
        $classname_ = '';
        foreach ( (array) $wp_registered_widgets[$id]['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'], $id, $classname_);         
        $params = apply_filters( 'dynamic_sidebar_params', $params );
    
        // Run the callback
        $callback = $wp_registered_widgets[$id]['callback'];            
        if ( is_callable( $callback ) )
        {
             call_user_func_array( $callback, $params );
             $did_one = TRUE;
        }
    
        return $did_one;
    }
    

    where you call it like this:

    wpse_show_widget( 'sidebar-1', 'calendar-2' );
    

    Another way would be to try to use the the_widget() function, where you would have to know the widget’s class name.

Comments are closed.