Enabling Widgets By Default in Custom Theme Development

In WP theme building, if a sidebar is not active, can I turn it on? Also, if the sidebar is active, and doesn’t contain any widgets, can I add some?

I’m a theme and plugin developer, but this is something I haven’t learned to do yet.

Related posts

Leave a Reply

1 comment

  1. @Volomike,

    When you register a sidebar in your themes functions it will be active in the dashboard. If no widgets are added to your sidebars WordPress will add the default widgets. You can prevent WordPress from adding the default widgets by unregistering them:

    // Remove WP default Widgets
        // WP 2.8 function using $widget_class
        if (function_exists('unregister_widget')) {
            unregister_widget('WP_Widget_Meta');
            unregister_widget('WP_Widget_Search');
            unregister_widget('');
    
        // pre WP 2.8 function using $id
        } else {
            unregister_widget_control('meta');
            unregister_widget_control('search');
        }
    

    You can also preset your own widgets.

    // The following code sample is from The Thematic Theme which is licensed under the GPLv2
    
        register_sidebar_widget(__('Search', 'thematic'), 'widget_thematic_search', null, 'search');
        unregister_widget_control('search');
        register_sidebar_widget(__('Meta', 'thematic'), 'widget_thematic_meta', null, 'meta');
        unregister_widget_control('meta');
        register_sidebar_widget(array(__('RSS Links', 'thematic'), 'widgets'), 'widget_thematic_rsslinks');
        register_widget_control(array(__('RSS Links', 'thematic'), 'widgets'), 'widget_thematic_rsslinks_control', 300, 90);
    
        // Pre-set Widgets
        $preset_widgets = array (
            'primary-aside'  => array( 'search', 'pages', 'categories', 'archives' ),
            'secondary-aside'  => array( 'links', 'rss-links', 'meta' )
            );
    
        if ( isset( $_GET['activated'] ) ) {
            update_option( 'sidebars_widgets', apply_filters('thematic_preset_widgets',$preset_widgets ));
        }
    

    Thematic is actually a great example of all the things that can be done with widgets. Take a look at widgets.php and widgets-extensions.php