How come the markup I specify in the 'before_title' and 'after_title' settings of register_sidebar gets applied to the default text WordPress widget but not one of our custom widgets, when both are in the same sidebar and both have a title in the widget settings?

For example when I set

            'name' => $name,
            'id' => $id,
            'before_widget' => '<li id="%1$s" class="widget %2$s">',
            'after_widget' => '</li>',
            'before_title' => '<h2 class="widgettitle">XXXXX',
            'after_title' => '</h2>',

the XXXX appears in the text widget's title, but not in any of our custom widgets.

In the widget method of the custom widgets we do stuff like

$title = $instance['title'];
if ( $title ) {
    echo '<h2><span class="text">' . esc_html( $title ) . '</span></h2>';

Does this way of rendering the title override any settings you put in register_sidebar?

  • The issue is, your custom widget is NOT using the arguments being passed by the sidebar. Refer to the answer below to see how it's done. Sep 18, 2013 at 13:44

1 Answer 1


If that is all of your "title" code, you aren't echoing $before_title and $after_title. Look at how the default text widget, which you reference, does it:

399         function widget( $args, $instance ) {
400                 extract($args);
401                 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
402                 $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
403                 echo $before_widget;
404                 if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
405                         <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
406                 <?php
407                 echo $after_widget;
408         }

Look at line 404. I don't see the "before" and "after" in your code.


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.