0

I am currently working on a theme that uses a Page Sidebar for the Contact, About, Portfolio and Home pages, and a Blog Sidebar for the Blog page.

For some reason, the Page Sidebar is not showing up on any of its respective pages. I ran my theme through a WordPress theme validator, which gave me the following error message:

"Sidebars need to be registered in a custom function hooked to the widgets_init action. See: register_sidebar()."

For reference, here is the code that I used to register and create both sidebars:

function wpt_create_widget( $name, $id, $description ) {

    register_sidebar(array(
        'name' => __( $name ),   
        'id' => $id, 
        'description' => __( $description ),
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="module-heading">',
        'after_title' => '</h2>'
    ));

}

wpt_create_widget( 'Page Sidebar', 'page', 'Displays on the side of pages with a sidebar' );
wpt_create_widget( 'Blog Sidebar', 'blog', 'Displays on the side of pages in the blog section' );

Can anyone give me advice as to what specific PHP files I should be focusing on, to correct the issue? The files that I've potentially narrowed it down to are:

functions.php
page-sidebar-left.php
sidebar.php (Blog Sidebar)
sidebar-page.php (Page Sidebar)

1 Answer 1

1

The error you are getting is because you must add the registered sidebar to widgets_init.

add_action( 'widgets_init', 'wpa199335_page_sidebar' );

function wpa199335_page_sidebar(){
    register_sidebar(array(
        'name' => __( 'Page Sidebar'),
        'id' => 'page',
        'description' => __( 'Displays on the side of pages with a sidebar' ),
        'before_widget' => '<div class="widget">',
        'after_widget' => '</div>',
        'before_title' => '<h2 class="module-heading">',
        'after_title' => '</h2>'
    ));
}

That will only register the area to put widgets into. For the sidebar to display on a page you will need to use the following in a template file or your functions.php.

dynamic_sidebar( 'page' );
1
  • Let me know if you have any questions. Commented Aug 25, 2015 at 18:41

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.