0

I have built a custom widget with this code:

class rfw_dock extends WP_Widget {
    function __construct() {
        parent::__construct('rfw_dock', __('TITLE'), array( 'description' => __( 'DESCRIPTION')));
    }
    function widget($args, $instance) {
        $title = $instance['title'];
        $rss_url = $instance['rss_url'];
        $rss = fetch_feed($rss_url);
        $rss_items = $rss->get_items( 0, 1 );
        echo $before_widget;
        if ( $title )
            echo $before_title . $title . $after_title;
        foreach ($rss_items as $item) {
            echo '<article><a href="'. esc_url( $item->get_permalink() ) .'" target="_blank" rel="nofollow"><h2>'. $item->get_title() .'</h2></a></article>';
        echo $after_widget;
        }
    }
    function form($instance) {
        $title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
        $rss_url     = isset( $instance['rss_url'] ) ? esc_attr( $instance['rss_url'] ) : '';
        $number    = isset( $instance['number'] ) ? absint( $instance['number'] ) : 6; ?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title (optional):' ); ?></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>
        <p><label for="<?php echo $this->get_field_id( 'rss_url' ); ?>"><?php _e('Enter the RSS feed URL here:'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'rss_url' ); ?>" name="<?php echo $this->get_field_name( 'rss_url' ); ?>" type="text" value="<?php echo esc_attr( $rss_url ); ?>" /></p><?php 
    }
    function update($new_instance, $old_instance) {
        $instance = array();
        $instance['title'] = strip_tags($new_instance['title']);
        $instance['rss_url'] = strip_tags($new_instance['rss_url']);
        $instance['number'] = (int) $new_instance['number'];
        $instance['show_feed_title'] = ($new_instance['show_feed_title']==true);
        $instance['feed_words'] = (int) $new_instance['feed_words'];
        $instance['content_display'] = strip_tags($new_instance['content_display']);
        return $instance;
    }
}
register_widget('rfw_dock');`

And registered this sidebar:

register_sidebar( array(
    'name' => __('S&oslash;stersider'),
    'before_widget' => '<div>',
    'after_widget' => '</div>',
    'before_title' => '<h6>',
    'after_title' => '</h6>'
) );

Pretty straight-forward, right? Well, my problem is that the $before_title, $after_title, $before_widget and $after_widget doesn't return anything, even though they're specified in the code. Isn't those variables how you're supposed to define the information in the register_sidebar? At least, it is in default-widgets.php.

I'm sorry, but I'm not the best in PHP, although I consider myself to have a medium-levelled knowledge in it. I've tried a lot before posting this, but nothing works. Can someone please help me out? Thanks.

0

I finally figured it out - I was missing the line extract($args); inside function widget($args, $instance). Now everything works as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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