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.

  • Don't you have a conflict with your sidebar ID and/or sidebar name – Pieter Goosen Aug 16 '14 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 '14 at 15:56
2

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

  • 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. – LoganGoesPlaces Nov 21 '17 at 16:20
  • @LoganGoesPlaces Thanks, I've added a note to : codex.wordpress.org/Function_Reference/… – T.Todua Nov 21 '17 at 16:37
  • This just fixed an issue I was having with a theme's custom sidebars. – Pikamander2 Aug 12 '18 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?

  • 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 '14 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
    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 '16 at 22:46

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.