Basic WordPress Feed importer, Widget version

Few days ago I was talking about one basic Feed importer I wrote to be used in WordPress templates, and the possibility of making a Widget out of it. Now here it is 🙂

<?php
class FriendsRSS extends WP_Widget {
    /** constructor */
    function FriendsRSS() {
        parent::WP_Widget( false, $name = 'FriendsRSS',
                           array( 'description' => 'Show latest posts from other websites using their feeds.' ) );  
    }

    /** @see WP_Widget::widget */
    function widget($args, $instance) {     
        extract( $args );
        $title = apply_filters('widget_title', $instance['title']);
        $friends = apply_filters('widget_title', $instance['friends']);
        $quantity = apply_filters('widget_title', $instance['quantity']);
        ?>
              <?php echo $before_widget; ?>
                  <?php if ( $title ) echo $before_title . $title . $after_title; ?>
<?php $display = ( ( isset($quantity) && $quantity != NULL ) ? ( $quantity > 15 ? 15 : (int) $quantity ) : 2 ); ?>
<?php if ( isset( $friends ) && $friends != NULL ) : $feeds = get_friends_feeds( explode( "n", $friends ), $display ); krsort( $feeds ); ?>
<?php if ( count( $feeds ) > 0 ) : ?>
			<ul>
<?php foreach($feeds as $feed) : $feed = (object) $feed; ?>
				<li>
					<?php echo $feed->_date; ?> : 
					<a class="permalink" href="<?php echo $feed->_href; ?>" title="<?php echo $feed->_atitle . '. ' . long_excerpt($feed->_desc, 25); ?>">
						<?php echo $feed->_title; ?>
					</a>
				</li>
<?php endforeach; ?>
			</ul>
<?php endif; ?>
<?php endif; ?>
              <?php echo $after_widget; ?>
        <?php
    }

    /** @see WP_Widget::update */
    function update($new_instance, $old_instance) {             
    $instance = $old_instance;
    $instance['title'] = strip_tags($new_instance['title']);
    $instance['friends'] = strip_tags($new_instance['friends']);
    $instance['quantity'] = strip_tags($new_instance['quantity']);
        return $instance;
    }

    /** @see WP_Widget::form */
    function form($instance) {              
        $title = esc_attr($instance['title']);
        $friends = esc_attr($instance['friends']);
        $quantity = esc_attr($instance['quantity']);
        ?>
        <p>
          <label for="<?= $this->get_field_id('title'); ?>"><?= _e('Title:'); ?></label> 
          <input class="widefat" id="<?= $this->get_field_id('title'); ?>" name="<?= $this->get_field_name('title'); ?>" type="text" value="<?= $title; ?>" />
        </p>
        <p>
          <label for="<?= $this->get_field_id('friends'); ?>"><?= _e('Feeds to include<br /><small>(one url per line)</small>'); ?></label> 
          <textarea class="widefat" id="<?= $this->get_field_id('friends'); ?>" name="<?= $this->get_field_name('friends'); ?>" style="width:220px;height:175px"><?= $friends; ?></textarea>
        </p>
        <p>
          <label for="<?= $this->get_field_id('quantity'); ?>"><?= _e('Number of posts to fetch<br /><small>(default is 2, maximum is 15)</small>'); ?></label> 
          <input class="widefat" id="<?= $this->get_field_id('quantity'); ?>" name="<?= $this->get_field_name('quantity'); ?>" type="text" value="<?= $quantity; ?>" />
        </p>
        <?php 
    }
}

add_action('widgets_init', create_function('', 'return register_widget("FriendsRSS");'));
?>

To be include in your theme’s functions.php file. I’ll let you go through WordPress’ Codex if you need more details on Widget developing. What we do here is simple enough: three options, title, friends and quantity. First is the Widget’s title to be displayed, second is the list of feeds to fetch, third is the number of feeds to fetch.

Really basic stuff, as usual feel free to modify whatever you want 🙂

Publié par Charlie

Être humain depuis 1986, développeur web, designer et photographe, je code pour le Web depuis 2000 et pour WordPress depuis 2008. Aventure, Histoire, sciences, musique, café ou personnages forts en caractère et dotés d'un nez en tout point remarquable sont parmi mes passions les plus dévorantes. Indépendant depuis 2010 je travaille avec des gens formidables dans le monde entier, de la Californie à l'Europe en passant par l'Australie et l'Asie. D'autres détails croustillants ?

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée.

*