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.

Here's how I have my code set up in the functions file:

register_sidebar(array(
    'name' => 'Latest Updates',
    'before_widget' => '<span>',
    'after_widget' => '</span>',
    'before_title' => '',
    'after_title' => ''
));
register_sidebar(array(
    'name' => 'BBRB Loves (Video Width - 200px)',
    'before_widget' => '',
    'after_widget' => '',
    'before_title' => '',
    'after_title' => ''
));

The first register_sidebar function gets called in header.php and the second function gets called in index.php. When I put in opening and closing <span> tags in the second function (i.e. before_widget and after_widget), the correct tags get outputted. But when I put in the same opening and closing <span> tags in the first function, nothing gets outputted but the default <div class="textwidget">foobar</div>. The only difference between the two is that one is called in the header and the other is called in the index. However, BOTH widgets show up in the dashboard and when I fill them with text, they both get outputted—just not the before/after tags. What am I doing wrong?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

If I had to venture a guess, the problem is that your first register_sidebar() gets called in functions.php, and hooked into widgets_init, but the second one gets called in the head, and therefore NOT hooked into widgets_init. I don't know the impact of registering a sidebar other than at widgets_init.

Is there any particular reason that both calls can't be called in functions.php and hooked into widgets_init?

share|improve this answer
    
Thanks for your input, @Chip. I suppose I can insert both into index.php so they get hooked into widgets_init. But then I'd need to get rid of the tags that Wordpress surrounds text widgets with (i.e. div class="textwidget"></div> Because right now, even if I add <span> tags before and after the widget, div class="textwidget"></div> gets outputted within those span tags. I suppose you don't know of a way to get rid of div class="textwidget"></div> altogether, do you? If I need to open a new question for this, please let me know. Thanks again for your help! –  jwp Mar 28 '11 at 3:29

You should register all your sidebars as a function and hook them into widgets_init.

If you want to get rid of <div class="text_widget"> </div> you will have to write your own text widget because there is not a filter for it.

in wp-includes/default-widgets.php here is how the text widget is built:

/**
 * Text widget class
 *
 * @since 2.8.0
 */
class WP_Widget_Text extends WP_Widget {

    function WP_Widget_Text() {
        $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
        $control_ops = array('width' => 400, 'height' => 350);
        $this->WP_Widget('text', __('Text'), $widget_ops, $control_ops);
    }

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

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        if ( current_user_can('unfiltered_html') )
            $instance['text'] =  $new_instance['text'];
        else
            $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
        $instance['filter'] = isset($new_instance['filter']);
        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags($instance['title']);
        $text = esc_textarea($instance['text']);
?>
        <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>

        <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>

        <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
    }
}

You can reuse some of the code to create your own version of a text widget just make sure you don't use any of the same class or function names.

share|improve this answer

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.