2

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...

  • You are asking about your code without showing it. How do you expect people to know what you are doing wrong? – Mark Kaplun Dec 15 '15 at 11:22
  • The only thing I can do is post all the code in the customizer.php file, and I am not comfortable doing that. I just want to know if there is some Wordpress feature that lets you enable that panel, not for my code to be debugged. – Kaspar Lee Dec 15 '15 at 12:48
  • No, the bug is in your code. – Mark Kaplun Dec 15 '15 at 13:15
  • No, I removed all the code from the customizer.php and the panel still did not show. – Kaspar Lee Dec 15 '15 at 13:27
  • Could you post the code you have for adding the widget areas? – Arcath Dec 15 '15 at 13:32
0

Most likely you are not displaying the sidebar when the costumizer is active which prevents the costumizer from detecting its existence on the page.

The costumizer detects the sidebar by hooking on various sidebar related hooks that are supposed to be triggered when the page is generated. If for some reason your sidebar display code do not trigger the hooks the costumizer will not know it is there even if the actual content is being displayed. This can happen for example if you cache your sidebar and instead of calling dynamic_display you just output the sidebar.

  • The sidebar is displayed - I added widgets through it from Appearance > Widgets, and I can see them on the page. Is the customizer maybe not detecting them? I have added it to the page using dynamic_sidebar( 'sidebar-1' ) – Kaspar Lee Dec 15 '15 at 13:47
  • I expanded a little – Mark Kaplun Dec 15 '15 at 16:58
  • Okay thanks. As I said earlier, I display my sidebar using dynamic_sidebar( 'sidebar-1' ). What do I need to do differently to get the customizer to detect it? – Kaspar Lee Dec 15 '15 at 18:54
0

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!

  • No, I was not using sidebar.php, because I had multiple sidebars. I was registering them in functions.php and calling them directly in index.php via dynamic_sidebar('sidebar-id'). I had created sidebar.php but it was completely blank. I tried using sidebar.php but it still did not show the Widgets panel, even after I copy + pasted your code – Kaspar Lee Dec 22 '15 at 16:49
  • 1
    Well, in that case there must be something else that you're overlooking. I'd advise you to update WordPress to the latest version and disable all plugins to narrow down the search, there may be a conflict somewhere. And If possible, get the theme on Github or Bitbucket repo so everyone else can have a look at the code. You'll probably get a solution faster. – Jentan Bernardus Dec 23 '15 at 18:16
  • I am on the latest version (4.4), with no plugins. Also, sorry, I cannot upload my theme files to GitHub for the public for various reasons (which is why I posted all the relevant code). – Kaspar Lee Dec 23 '15 at 19:29
  • 1
    @Druzion I'm afraid I can't help you further. I did my best but It's difficult to propose solution to a problem I can't replicate. :( – Jentan Bernardus Dec 24 '15 at 2:30

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.