Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

register_sidebar(
        array(
            '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?

share|improve this question
    
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. –  Eric Holmes Sep 18 '13 at 13:44
    
Thanks Eric, I get it now. –  And Finally Sep 18 '13 at 14:10

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Great, that explains it - thanks @s_ha_dum. –  And Finally Sep 18 '13 at 14:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.