Steve Taylor photo

WordPress feeds mashup widget

I’ve put together a few little widgets in my time for client sites, dropped into my custom theme’s functions.php. I might look into putting them out there in a formalized way some time (plugins? a collection of code snippets?), but for now here’s one I just did that I think is pretty useful. It basically takes one or more feed URLs, and outputs the most recent items after merging the feeds together.

The code for getting and outputting the feed(s) is based on Jeff Starr’s typically simple and thorough code. It uses the SimplePie API which comes with WordPress, and has automatic caching—which is nice.

Options are pretty self-explanatory, and CSS classes are provided for styles to hook onto. So, custom theme developers, drop this into your functions.php. Please post any bugs, ideas, and suggestions!

class slt_Feed_Mashup_Widget extends WP_Widget {
// Initialize
function slt_Feed_Mashup_Widget() {
$widget_ops = array( ‘classname’ => ‘slt-feed-mashup’, ‘description’ => ‘Aggregate multiple feeds into one stream of updates’ );
$this->WP_Widget( ‘slt-feed-mashup’, ‘Feed Mashup’, $widget_ops );
}
// Display
function widget( $args, $instance ) {
extract( $args );
if ( function_exists( ‘fetch_feed’ ) && $instance[‘urls’] ) {
include_once( ABSPATH . WPINC . ‘/feed.php’ );
$feed = fetch_feed( array_filter( explode( "n", $instance[‘urls’] ) ) );
$limit = $feed->get_item_quantity( $instance[‘number’] );
$items = $feed->get_items( 0, $limit );
if ( $limit > 0 ) {
echo $before_widget;
$title = apply_filters( ‘widget_title’, $instance[‘title’] );
if ( $title ) echo $before_title . $title . $after_title;
echo ‘

    ‘;
    foreach ( $items as $item ) {
    echo ‘

  • get_permalink() . ‘" title="’ . $item->get_date(‘j F Y @ g:i a’) . ‘">’ . $item->get_title() . ‘‘;
    if ( $instance[‘source’] ) {
    $source_parts = explode( ‘/’, $item->get_permalink() );
    if ( $source_parts[2] )
    echo ‘ ‘ . $source_parts[2] . ‘‘;
    }
    if ( $instance[‘extract’] ) {
    $extract = $item->get_description();
    if ( $instance[‘extract-max-words’] ) {
    $words = explode( " ", $extract );
    if ( count( $words ) > $instance[‘extract-max-words’] ) {
    $words = array_slice( $words, 0, $instance[‘extract-max-words’] );
    $extract = implode( " ", $words ) . ‘ …’;
    }
    }
    echo ‘

    ‘ . $extract . ‘

    ‘;
    }
    echo ‘

  • ‘;
    }
    echo ‘

‘;
echo $after_widget;
}
}
}
// Update
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[‘title’] = strip_tags( $new_instance[‘title’] );
$instance[‘urls’] = strip_tags( $new_instance[‘urls’] );
$instance[‘source’] = $new_instance[‘source’];
$instance[‘extract’] = $new_instance[‘extract’];
$instance[‘extract-max-words’] = $new_instance[‘extract-max-words’];
$instance[‘number’] = $new_instance[‘number’];
return $instance;
}
function form( $instance ) {
$defaults = array( ‘title’ => ”, ‘urls’ => ”, ‘number’ => 5 );
$instance = wp_parse_args( (array) $instance, $defaults );
?>


get_field_id( ‘title’ ); ?>" name="get_field_name( ‘title’ ); ?>" value="" style="width:90%;" />



get_field_id( ‘source’ ); ?>" name="get_field_name( ‘source’ ); ?>" />


get_field_id( ‘extract’ ); ?>" name="get_field_name( ‘extract’ ); ?>" />


get_field_id( ‘extract-max-words’ ); ?>" name="get_field_name( ‘extract-max-words’ ); ?>" size="2" value="" />


2 comments

  1. Dan avatar Dan

    Great stuff Steve

    Your two most recent posts have both alerted me to WP features I wasn’t previously aware of, so d’uh from me too!

    Wishing you all the best for 2011!

  2. All the best to you too :) I’ve just been getting in the habit of searching through the core code more when I need to do something. Takes up more time in the short term, but a great habit!

Comments are closed.