register_sidebar ‘after_widget’ on custom-built widgets not implementing, caused nested widgets

In registering my sidebar I want each widget wrapped in <aside>. This works fine when using the inbuilt widgets like Calendar or search, however, when I add widgets I have created myself, the closing <aside> is not getting output. Therefore I’m getting a structure similar to:

<widgets-area>
    <custom-widget>
        <custom-widget>
        </custom-widget>
    </custom-widget>
</widgets-area>

When it should be looking like:

Read More
<widgets-area>
    <custom-widget>
    </custom-widget>
    <custom-widget>
    </custom-widget>
</widgets-area>

register_sidebar

register_sidebar( array(
    'name'          => __( 'Sidebar', 'mysite' ),
    'id'            => 'sidebar-1',
    'before_widget' => '<aside id="%1$s" class="widgets__widget widgets__widget--%2$s">',
    'after_widget'  => '</aside>',
    'before_title'  => '<h1 class="widgets__widget__title">',
    'after_title'   => '</h1>',
) );

The code for my custom widgets include both echo $args['before_widget'] and echo $args['after_widget'] which I presume is used to echo out the ‘before_widget’ and ‘after_widget’ code declared in register_sidebar().

Why are my custom widgets not closing properly, but the built-in ones are? What am I missing? Other than the structure, the custom widgets are functioning correctly.

Related posts

1 comment

  1. It turns out I had overwritten the widget($args) within the widget, hence the before_widget got echoed but not the after_widget.

    I renamed the widget($args) variable, and all references to it, then the plugins functioned as expected.

Comments are closed.