1

I'm working on a child theme of the Toolset Bootstrap Theme. I need to create a custom sidebar, so I registered the new sidebar in my child theme's function.php file:

function wpbootstrap_register_newsidebar() {

/* Register the new sidebar. */
register_sidebar(
    array(
        'id' => 'sidebar-999',
        'name' => __( 'Home sidebar', 'wpbootstrap' ),
        'description' => __( 'This sidebar will display only on the homepage.', 'wpbootstrap' ),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
    )
);
} add_action( 'widgets_init', 'wpbootstrap_register_newsidebar' );

I can see the new sidebar in the admin screen, drag widgets into it, add a title, reorder them but if I reload the page all of the widget disappear from the sidebar. The parent theme already has 3 built-in widgetized areas, which work just fine: I can add and save widgets in those areas. Also if I switch to a default theme, like Twenty Fourteen, and try to register a custom sidebar using the same code pasted above, I get a fully functional custom sidebar area. So apparently my code is not the problem.

I tried to post the question on the parent theme's vendor support forum, but haven't heard from them yet, so I thought to ask to the wider WP community just in case someone is experiencing a similar problem.

I'm not a WP guru and I don't know much about how custom widgetized area are created and stored inside WP environment, but if someone can point me to the right direction I'd be more than happy to learn some more about it.

Unfortunately I'm working on a local development version of a website, so I cannot provide any testing URL.

2
  • Don't you have a conflict with your sidebar ID and/or sidebar name Aug 16, 2014 at 17:33
  • Thanks @PieterGoosen that was my first thought. I tried with different names, every time made sure that ID/name is unique (my code editor allows me to search all the files in the theme folder). But the code still doesn't work as expected, at least not with this theme.
    – kikaweb
    Aug 17, 2014 at 15:56

4 Answers 4

2

the ID of sidebar (in php function) should be LOWERCASE!!!!!!!!!!!

3
  • 1
    I was having the exact same problem with widgets disappearing and THIS was the solution. It would be cool if the codex page about registering custom widget areas mentioned this. Nov 21, 2017 at 16:20
  • @LoganGoesPlaces Thanks, I've added a note to : codex.wordpress.org/Function_Reference/…
    – T.Todua
    Nov 21, 2017 at 16:37
  • This just fixed an issue I was having with a theme's custom sidebars. Aug 12, 2018 at 1:47
1

This seems to test properly for me.

function register_home_sidebar() {

/* Register the new sidebar. */
register_sidebar(
    array(
        'id' => 'home-sidebar',
        'name' => __('Home Sidebar'),
        'description' => __( 'This sidebar will display only on the homepage.'),
        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
        'after_widget' => '</aside>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>'
          )
     );
} add_action( 'widgets_init', 'register_home_sidebar' );

Maybe you have a function already called by the other name?

1
  • Thanks @howard-e I tried your code but it doesn't work in this specific theme. Just like my exemple, it works if used with the Twenty Fourteen theme, though. Something in this theme prevents the sidebar to be saved, but I don't know much about WP to debug it!
    – kikaweb
    Aug 17, 2014 at 15:50
1

Ok, finally the support team answered my question, and if even if there's not a real solution, suggested a workaround. Since the answer is buried deep in the vendor user forum - link here - I'm sharing the solution here, just in case someone has the same problem with this or another theme.

Basically it's a bug in the parent theme (Toolset Bootstrap), that resets the custom sidebars. To override this behaviour I used this snippet of code to register my sidebars, as suggested by support team:

//Override check sidebar widgets filter in child theme
function wpbootstrap_checksidebars_widgets($sidebars_widgets) {
    return $sidebars_widgets;
}
add_filter('sidebars_widgets','wpbootstrap_checksidebars_widgets' );
//Now user can add custom widget areas
function custom_widget_areas_init() {
    register_sidebar( array(
    'name' => 'Front Page Widget Area',
    'id' => 'frontpage-widgets',
    'before_widget' => '<div>',
    'after_widget' => '</div>',
    'before_title' => '<h2 class="rounded">',
    'after_title' => '</h2>',
    ) );
}
add_action( 'widgets_init', 'custom_widget_areas_init' );

Now the widgets stay in the newly registered sidebars. Thanks anyway to the users who tried to help me.

-1
function wpbootstrap_checksidebars_widgets($sidebars_widgets) {
    return $sidebars_widgets;
}
add_filter('sidebars_widgets','wpbootstrap_checksidebars_widgets' );
//Now user can add custom widget areas
function custom_widget_areas_init() {
    register_sidebar( array(
    'name' => 'Front Page Widget Area',
    'id' => 'frontpage-widgets',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '',
    'after_title' => '',
    ) );
}
add_action( 'widgets_init', 'custom_widget_areas_init' );
1
  • 1
    Answers should be more than a code snippet. Please improve your answer with an edit explaining what your code does and how it answers the question.
    – Gabriel
    May 12, 2016 at 22:46

Your Answer

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

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