How to avoid widgets added to sidebar on theme activation?

Widgets are stored in the wp_options table ( assuming the database prefix is wp_ ). You can retrieve the option, and remove the widgets manually:

// Get all the associated widgets
$sidebar_widgets = get_option ( 'sidebars_widgets' );

// Check this specific sidebar
if ( isset( $sidebar_widgets [ 'lateral-header-sidebar' ] ) ) {
    unset ( $sidebar_widgets [ 'lateral-header-sidebar' ] );
    // Update the option
    update_option ( 'sidebars_widgets', $sidebar_widgets ); 
}

You can do this in your theme activation hook.

Leave a Comment