/ 设置 10 秒超时 // 每日统计清 0 runtime_set('todaycomments', 0); runtime_set('todayarticles', 0); runtime_set('todayusers', 0); if ($forumlist) { $fidarr = array(); foreach ($forumlist as $fid => $forum) { $fidarr[] = $forum['fid']; } forum_update($fidarr, array('todayposts' => 0, 'todaythreads' => 0)); } // 清理临时附件 attach_gc(); // 当天24点 $today = strtotime(date('Ymd')) + 86400; runtime_set('cron_2_last_date', $today, TRUE); // 往前推8个小时,尽量保证在前一天 升级过来和采集的数据会很卡 // table_day_cron($time - 8 * 3600); cache_delete('cron_lock_2'); } } } ?>How to avoid widgets added to sidebar on theme activation?|Concepts Of Algorithm

How to avoid widgets added to sidebar on theme activation?

admin2025-06-07  10

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

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

Any help is appreciated.

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/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.

Share Improve this question asked Oct 23, 2018 at 9:32 BennBenn 1,0731 gold badge15 silver badges32 bronze badges 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/questions/11757461/… Or are there any plugins that might be the cause? – Antti Koskinen Commented Oct 25, 2018 at 10:52
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749230712a317458.html

最新回复(0)