Default widgets assignment – isn’t working

The question is regarding, calling the default widgets into the sidebar[s]:

So, despite the solution given in THIS THREAD, I tried a different way – Ian Stewart’s borrowed solution given HERE.

Read More

It’s like, declaring the following lines into functions.php, suppose I have two different sidebars in my theme:

// Preset Widgets
$preset_widgets = array (
        'primary_widget_area'  => array( 'search', 'pages', 'categories', 'archives' ),
        'secondary_widget_area'  => array( 'links', 'meta' )
);

if ( isset( $_GET['activated'] ) ) {
        update_option( 'sidebars_widgets', $preset_widgets );
}

//update_option( 'sidebars_widgets', NULL );




// Check for static widgets in widget-ready areas
function is_sidebar_active( $index ){

  global $wp_registered_sidebars;

  $widgetcolums = wp_get_sidebars_widgets();

  if ($widgetcolums[$index]) return true;

        return false;

} // end is_sidebar_active

The problem is, my theme is NOT calling the default widgets in absence of any widgets at the beginning. I can’t fix it, because I can’t understand the code above and obviously the process explained by Ian Stewart.

And also, can’t understand the explanation regarding //update_option( 'sidebars_widgets', NULL );.

How can I fix this?

Related posts

1 comment

  1. I solved the issue using a fallback with PHP conditionals: in my sidebar.php I coded like the following:

    <?php if( is_active_sidebar('my_sidebar') ) { ?>
    
            <div id="secondary" class="widget-area">
                <ul class="xoxo">
                    <?php dynamic_sidebar('my_sidebar'); ?>
                </ul>
            </div><!-- #secondary .widget-area -->
    
    <?php } else { ?>
    
            <div id="secondary" class="widget-area">
                <ul class="xoxo">
    
                    <?php // Default Widget : Archives ?>
                    <li id="default-widget-1" class="widget-container default-widgets">
                        <h3 class="widget-title"><?php _e( 'Archives', 'your-theme' ); ?></h3>
                        <ul>
                            <?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
                        </ul>
                    </li> <!-- #default-widget-1 -->
    
                    <?php // Default Widget : Pages ?>
                    <li id="default-widget-2" class="widget-container default-widgets">
                        <h3 class="widget-title"><?php _e( 'Pages', 'your-theme' ); ?></h3>
                        <ul>
                            <?php wp_list_pages('sort_column=menu_order&title_li='); ?>
                        </ul>
                    </li> <!-- #default-widget-2 -->
    
                    <?php // Default Widget : Categories ?>
                    <li id="default-widget-3" class="widget-container default-widgets">
                        <h3 class="widget-title"><?php _e( 'Categories', 'your-theme' ); ?></h3>                            
                        <ul>
                            <?php wp_list_categories( 'sort_column=menu_order&title_li=' ); ?>
                        </ul>
                    </li> <!-- #default-widget-3 -->
    
                    <?php // Default Widget : Meta ?>
                    <li id="default-widget-4" class="widget-container default-widgets">
                        <h3 class="widget-title"><?php _e( 'Meta', 'your-theme' ); ?></h3>                            
                        <ul>
                            <?php wp_register(); ?>
                            <li><?php wp_loginout(); ?></li>
                            <?php wp_meta(); ?>
                        </ul>
                    </li> <!-- #default-widget-4 -->
    
                </ul> <!-- .xoxo -->
    
            </div><!-- #secondary .widget-area -->
    
    <?php } //endif( is_active_sidebar ?>
    

Comments are closed.