3

I am registering 3 sidebars on widgets_init

    register_sidebar(array(
        'name' =>  esc_html__('Left Sidebar','creatus'),
        'id' => 'left',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<div class="widget_title_holder"><h3 class="widget-title">',
        'after_title' => '</h3></div>',
    )); 

   // right sidebar
    register_sidebar(array(
        'name' => esc_html__('Right Sidebar','creatus'),
        'id' => 'right',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<div class="widget_title_holder"><h3 class="widget-title">',
        'after_title' => '</h3></div>',
    ));

    // lateral header sidebar
    register_sidebar(array(
        'name' => esc_html__('Lateral header sidebar','creatus'),
        'id' => 'lateral-header-sidebar',
        'before_widget' => '<div id="%1$s" class="widget %2$s">',
        'after_widget' => '</div>',
        'before_title' => '<div class="widget_title_holder"><h3 class="widget-title">',
        'after_title' => '</h3></div>',
    ));

When I activate the theme for the first time on a clean WP install 6 widgets get automatically added to lateral-header-sidebar

http://prntscr.com/l9cx84

Am I registering them the wrong way or why are they added to this particular sidebar and not any other?

Any help is appreciated.

1
  • Your registration code looks fine to me. How's the rest of your theme code? Are there any other widget related functions that might hook into after_switch_theme for example? Something along these lines, stackoverflow.com/questions/11757461/… Or are there any plugins that might be the cause? Commented Oct 25, 2018 at 10:52

1 Answer 1

1

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.

1
  • thnx for that but it does not explain the actual issue. Why are they added in the first place? Cleaning up on theme install is not what we should be doing.
    – Benn
    Commented Nov 2, 2018 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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