Link to home
Start Free TrialLog in
Avatar of Chris Andrews
Chris AndrewsFlag for United States of America

asked on

Add rss cache expiration to wp rss widget plugin

I want to use an rss display widget plugin called Alan's BBC Technology News widget:

http://wordpress.org/extend/plugins/bbc-tech-news-rss-feed-widget/ 

I'll be changing the bbc feed url to another feed, instructions for doing that are included with the plugin, that's not an issue.

However, the feed I do what to use is frequently updated.

Is there a way to add code into this plugin that would make the widget's cache expire after 10 minutes?

I did find a similar plugin that does have a setting for a time in which the cache expires. That plugin is here: http://wordpress.org/extend/plugins/better-rss-widget/ 

However, I prefer to use Alan's plugin, as except for the cache, it does just want I need (hardcode the url in). I want the cache expiration time hardcoded in as well.

The php code for the plugin I want to use is attached - can you change it to have the rss widget cache to expire after 10 minutes?

Thanks,

Chris
bbctechnews.php
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I always worry about the quality of a piece of code when the author spells Technology like this: Technolgy :-(

But that aside, there is nothing in this code that will cache the RSS feed.  It appears that the feed is acquired near line 50 via a function call to fetch_feed().  Unless that function has a cacheing mechanism, my assumption would be that the feed is acquired anew each time the page is built.

<?php
/*
Plugin Name: rss Technolgy News Widget
Plugin URI: http://hyperstream.co.uk/rss
Description: This plugin will show the latest 5 technolgy news stories from BBC technolgy rss feed. It's a very simple RSS reader widget. You can modify the plugin to suit your own purposes.
Version: 0.1
Author: Alan Son
Author URI: http://www.hyperstream.co.uk
 */

/*  Copyright 2011 Alan Son (email : alan@hyperstream.co.uk)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

 class rssfeedWidget extends WP_Widget
{
	function rssfeedWidget()
	{
		$widget_options = array(
			'classname'		=>	'rss-feed-widget',
			'description'	=>	'Show the last 3 stories from rss ');
			
		parent::WP_Widget('rss_feed_widget','rssfeedWidget', $widget_options);
	}
	
	function widget($args, $instance)
	{
		extract( $args, EXTR_SKIP );
		$title = ( $instance['title'] ) ? $instance['title'] : 'Technolgy News';
		?>
		<?php echo $before_widget; ?>
		<?php echo $before_title . $title . $after_title ?>

<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');

// Get a SimplePie feed object from the specified feed source.
$rss = fetch_feed('http://feeds.rssi.co.uk/news/technology/rss.xml');
if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 
    // Figure out how many total items there are, but limit it to 5. 
    $maxitems = $rss->get_item_quantity(5); 

    // Build an array of all the items, starting with element 0 (first element).
    $rss_items = $rss->get_items(0, $maxitems); 
endif;
?>

<ul>
    <?php if ($maxitems == 0) echo '<li>No items.</li>';
    else
    // Loop through each feed item and display each item as a hyperlink.
    foreach ( $rss_items as $item ) : ?>
    <li>
        <a href='<?php echo $item->get_permalink(); ?>'
        title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
        <?php echo $item->get_title(); ?></a>
    </li>
    <?php endforeach; ?>
</ul>
		<?php 
	}

}

function rss_feed_init()
{
	register_widget("rssfeedWidget");
}
add_action('widgets_init','rss_feed_init');

Open in new window

Avatar of Chris Andrews

ASKER

Technolgy - yeh, I know ;)

ah, ok, I looked up fetch_feed(), apparently it caches it for 12 hours!

http://codex.wordpress.org/Function_Reference/fetch_feed 

It says I can modify the cache time with this filter:

http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_feed_cache_transient_lifetime 

but I'm not sure if they mean I would have to alter the wordpress source files to do that (which I would prefer not to do) or if that filter can be added to the plugin code somehow...

Chris
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Testing....

Would I need to put the number of seconds in that first () or did you set 2 hours in some other manner?
no, that breaks it :)
Oh, I think I see, you set  it to 1 second. I think it's working :)  Will keep testing -
Yes, I set it to one second, which would be about the same as not cacheing at all.
Working perfectly - thank you so much, that was a critical piece of the puzzle I was trying to put together for my site :)

Chris
Thanks for the points, and thanks for using EE! ~Ray