Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

@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

share|improve this answer
1  
Thematic can be a little blunt and completely ignore a lot of built in WordPress functionality. This may be for backward compatibility but you have to cut the chord some time. Instead of checking $_GET['activated'] use the built in API and use the switch_theme action to test the installed widgets and make your necessary changes. –  Gipetto Sep 18 '10 at 6:54
    
The final solution is here: wordpress.stackexchange.com/questions/16603/… –  Volomike May 7 '11 at 4:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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