0

I am trying to get rid of the following error in debug:

Notice: register_sidebar was called <strong>incorrectly</strong>. 
No <code>id</code> was set in the arguments array for the "Header" sidebar.

So when I register that sidebar I added the id parameter like so:

register_sidebar(array(
    'id' => 'header-sidebar',
    'name' => 'Header',
    'before_widget' => '<div class="widget">',
    'after_widget' => '</div>',
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
));

Now no matter if I use the name field or the id field dynamic_sidebar always returns false.

I've tried:

dynamic_sidebar( 'Header' );

and

dynamic_sidebar( 'header-sidebar' );

My understand is both of the above should work. Note if I comment out the id parameter in the register_sidebar call and use dynamic_sidebar( 'Header' ); everything works fine. What am I missing?

This is WP 4.2.2.

So per these directions: List all sidebar names? I was able to list all the sidebar names and when I set the id parameter with register_sidebar the id in the list of names changes. The specific sidebar I'm playing with now is 'sidebar-5' before setting an id and 'sidebar-jsp' after. Once the id field is set I can not use dynamic_sidebar with the name 'Sidebar', the original id 'sidebar-5' or the new id 'sidebar-jsp'.

Per s_ha_dum's suggestion I wrapped a single sidebar inside a function so that I could make it happen after 'widgets_init'. This did not work.

function register_header_sidebar_jsp() {
    register_sidebar(array(
        'name' => 'Sidebar',
        'id' => 'sidebar-jsp',
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ));
}
add_action( 'widgets_init', 'register_header_sidebar_jsp' );

I'm calling it right now with:

<?php var_dump(dynamic_sidebar('sidebar-jsp')); ?>

I'm not sure what's going on but I don't have all day to mess with this. Thanks for your suggestions everyone.

0

3 Answers 3

1

Your code works when I test it. I can only assume that the registration code is being hooked into the system too early or too late. register_sidebar() should be hooked on widgets_init per the Codex. Therefore:

function register_header_sidebar_wpse_190618() {
  register_sidebar(
    array(
      'id' => 'header-sidebar',
      'name' => 'Header',
      'before_widget' => '<div class="widget">',
      'after_widget' => '</div>',
      'before_title' => '<h3 class="widget-title">',
      'after_title' => '</h3>',
    )
  );
}
add_action( 'widgets_init', 'register_header_sidebar_wpse_190618' );
5
  • I'm glad it works for someone.
    – JSP
    Jun 5, 2015 at 21:06
  • Something is broken about your site. You add a widget to the sidebar right? Or does it not show up on the back end at all?
    – s_ha_dum
    Jun 5, 2015 at 21:15
  • Yes, everything works fine if I don't specify an 'id' parameter. I only added the 'id' parameter to get rid of debug error messages but this is clearly worse. It shows up in the backend under Appearance > Widgets and again is clearly 'registered'. But again dynamic_sidebar always returns false when I add an id parameter to register_sidebar.
    – JSP
    Jun 5, 2015 at 21:57
  • Is there already a sidebar with that I'd?
    – s_ha_dum
    Jun 5, 2015 at 22:04
  • I'd qualify this as a bug. @Tosho has a good answer that will most likely provide more insight here... Aug 1, 2015 at 16:10
0
<?php dynamic_sidebar( 'new-header-sidebar' ); ?>

register_sidebar( array(
    'name' => __( 'Header', 'theme-slug' ),
    'id' => 'new-header-sidebar',
) );

You may have a I.D conflict so try changing the I.D

4
  • No idea why but this did not work for me. I'm not sure if the suggestion was more about using __() or removing the other parameters but I copy and pasted this with no luck.
    – JSP
    Jun 5, 2015 at 20:56
  • Here's the full code which is tested and works wordpress.stackexchange.com/a/190611/9884 Jun 6, 2015 at 4:47
  • 1
    Please don't just dump some code, and add some explanation. Code-only answers are not good, I'm afraid. Jun 6, 2015 at 10:48
  • I did add the explanation as a comment. I think you must have missed that. Jun 6, 2015 at 14:11
-1

Try starting the id field value with 'sidebar'.

If that doesn't work, the order shouldn't matter, but try following the codex example:

register_sidebar( array(
    'name' => __( 'Main Sidebar', 'theme-slug' ),
    'id' => 'sidebar-1',
    'description' => __( 'show on all posts and pages', 'theme-slug' ),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget'  => '</li>',
    'before_title'  => '<h2 class="widgettitle">',
    'after_title'   => '</h2>',
) );

Also try not using the word 'header' in the id field.

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.