0

I want to deactivate all default widgets on theme activation which are active by default for new WordPress installation.

I'm using following function as callback for theme activation :

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
    // 
}

By default activated widgets are :

  1. Search

  2. Recent Posts

  3. Recent Comments

  4. Archives

  5. Categories

  6. Meta

I don't want to deregister them permanently but hide from the sidebar.

Is that possible ??

2

Default widgets are not usually activated with the theme. Themes add default widgets in the sidebar template file. For example take a look at twenty ten sidebar.php file:

<?php
/**
 * The Sidebar containing the primary and secondary widget areas.
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */
?>
<div id="primary" class="widget-area" role="complementary">
    <ul class="xoxo">

<?php
    /* When we call the dynamic_sidebar() function, it'll spit out
     * the widgets for that widget area. If it instead returns false,
     * then the sidebar simply doesn't exist, so we'll hard-code in
     * some default sidebar stuff just in case.
     */
    if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?>

            <li id="search" class="widget-container widget_search">
                <?php get_search_form(); ?>
            </li>
            <li id="archives" class="widget-container">
                <h3 class="widget-title"><?php _e( 'Archives', 'twentyten' ); ?></h3>
                <ul>
                    <?php wp_get_archives( 'type=monthly' ); ?>
                </ul>
            </li>
            <li id="meta" class="widget-container">
                <h3 class="widget-title"><?php _e( 'Meta', 'twentyten' ); ?></h3>
                <ul>
                    <?php wp_register(); ?>
                    <li><?php wp_loginout(); ?></li>
                    <?php wp_meta(); ?>
                </ul>
            </li>
        <?php endif; // end primary widget area ?>
    </ul>
</div><!-- #primary .widget-area -->

as you can see the default widgets (which in this case are: search, archives and meta) are hard coded inside a dynamic_sidebar conditional check to see if this sidebar has any widgets set by the user.

So to answer your question make sure your sidebar file has no default widgets inside the dynamic_sidebar() conditional check.

Update:

I'm strongly against removing the configured and saved widgets of a previously used theme and think that you should not do it but if you must then you can catch the theme activation and trigger a simple function to clear saved widgets ex:

if ( is_admin() && isset($_GET['activated'] ) && $pagenow == 'themes.php' ) {
    add_action('admin_footer','removed_widgets');
}


function removed_widgets(){
    //get all registered sidebars
    global $wp_registered_sidebars;
    //get saved widgets
    $widgets = get_option('sidebars_widgets');
    //loop over the sidebars and remove all widgets
    foreach ($wp_registered_sidebars as $sidebar => $value) {
        unset($widgets[$sidebar]);
    }
    //update with widgets removed
    update_option('sidebars_widgets',$widgets);
}
  • I agree with you. But my issue is I see pre-activated widgets for my fresh wp-installation inside Appearance=>widgets=> "any-sidebar-1" as listed above. How to remove them on theme activation ? – MANnDAaR Jun 12 '13 at 14:14
  • @MANnDAaR i updated my answer. – Bainternet Jun 12 '13 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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