I am using the Theme Customizer to let users customize how their website looks.

I noticed on the Twenty Fifteen theme there is a panel for Widgets. I have seen this on quite a few other themes too, and the code for the panel has not been added to customizer.php (as far as I can tell)

On my theme, I have a few sidebars on the homepage. You can customize the widgets through Appearance > Widgets menus, however the Widgets panel in the customizer is not displaying.

How can I get it to show in the customizer so the user does not have to keep switching out to change the widgets?

My code for registering the sidebar:

function widgets_init_mysite() {
    register_sidebar( array(
        'name' => __( 'Main Sidebar', 'mytheme' ),
        'id' => 'sidebar-1',
        'before_widget' => '<div>',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="widget-title">',
        'after_title' => '</h3>',
    ) );
}

add_action( 'widgets_init', 'widgets_init_mysite' );

I add the sidebar to the page using dynamic_sidebar( 'sidebar-1' )

It is definitely displayed because I added widgets through Appearance > Widgets and I can see them in the customizer.

Note: One interesting thing I did find. I registered 5 Sidebars, with IDs of sidebar-1, sidebar-2 etc. In Firefix, I went to the theme customizer and Inspect Element. I found the Widgets panel existed, but had display: none. What is more interesting, in the ul sub-navigation, there were 5 li elements with the class section-sidebar-widgets-sidebar-1 (the last number changed for all the sidebars).

I checked the other sections I had made, and the class always started with section-, and then the section ID. I tried changing the panel of the sidebars to my panel like so:

$wp_customize->get_section( 'sidebar-widgets-sidebar-1' )->panel="my-panel";

But nothing happened. This is weird because I know pretty much definitely know the names of the Sidebar Sections, but changing their panel does nothing…

2 Answers
2

I see you’re talking about the dynamic_sidebar function but I don’t see you mention the sidebar file (eg. sidebar.php or sidebar-single.php).

There are basically 3 steps I follow to display sidebar widgets and they’re always visible in customizer. If yours is not showing up, you probably may have missed something.

1. The register part in functions.php


$args = array(
    'name'          => __( 'Main Sidebar', 'mytheme' ),
    'id'            => 'sidebar-1',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget'  => '</div>',
    'before_title'  => '<h3 class="widget-title">',
    'after_title'   => '</h3>'
);
register_sidebar( $args );

2. The function call in a sidebar file (eg. sidebar.php or sidebar-single.php)


<?php
// Dynamic Sidebar
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar-1') ) :
    // Sidebar fallback content
    get_sidebar();
endif;
// End Dynamic Sidebar Single posts
?>

3. Call the sidebar in the post/page template


<?php get_sidebar(); ?> or <?php get_sidebar('single'); ?> for sidebar-single.php

I would advise you to re-check your code to make sure you haven’t left anything out. All the best!

Leave a Reply

Your email address will not be published. Required fields are marked *