0
votes

I'm working on creating a WordPress blog using my own custom theme and am having difficulty displaying widgets in the sidebar. I have registered my sidebar in functions.php, and am using get_sidebar(); in index.php. If I put content in sidebar.php it will correctly display in the sidebar, but I want to be able to modify the sidebar dynamically. However, when I go to Appearance > Widgets, it shows my sidebar name, but no widgets are listed in the "Available Widgets" section.

This is where I register my sidebar in functions.php:

    function blog_widgets_init() {
        register_sidebar( array(
            'name'          => esc_html__( 'Sidebar', 'blog' ),
            'id'            => 'sidebar-1',
            'description'   => '',
            'before_widget' => '<section id="%1$s" class="widget %2$s">',
            'after_widget'  => '</section>',
            'before_title'  => '<h4 class="widget-title">',
            'after_title'   => '</h4>',
        ) );
    }
    add_action( 'widgets_init', 'blog_widgets_init' );

This is where I call the sidebar in index.php:

    <?php get_sidebar(); ?>

And this is sidebar.php:

    <?php 
        if ( ! is_active_sidebar( 'sidebar-1' ) ) {
            return;
        }
    ?>

    <div id="secondary" class="widget-area" role="complementary">
        <?php dynamic_sidebar( 'sidebar-1' ); ?>
    </div>

Any ideas why there no widgets available in Appearance > Widgets?

Edit: Solved it! There was actually a place in my code where the default widgets were being disabled. I didn't realise that was there, as it must've been included in the theme that I was basing mine off of. Removed those functions and the widgets show up again.

1
use the new widget api: codex.wordpress.org/Widgets_APISysix

1 Answers

0
votes

I think part of the issue is the call to is_active_sidebar()

From the documentation of that function:

Any sidebar that contains widgets will return TRUE, whereas any sidebar that does not contain any widgets will return FALSE.

So your first line, since that sidebar contains no widgets (yet), will always just "return" and end any further processing in sidebar.php. I'd probably just remove that whole block all together.

I don't exactly know why you're not seeing widgets show up in the admin area, but I'd be curious what happens if you retry after the above change.