admin管理员组文章数量:1389754
I'd like to decrease the default RSS cache time for a widget plugin i've made that outputs RSS feeds. I'm no php expert, I just copy and paste and understand little bits. I'm using this code to output the RSS
function widget($args, $instance) {
// outputs the content of the widget
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$tip_language = apply_filters('widget_title', $instance['tip_language']);
echo $before_widget;
echo "<h3 class=\"widgettitle\">" . $title . "</h3>";
include_once(ABSPATH . WPINC . '/rss.php');
$rss_feed = "";
$rss = fetch_rss($rss_feed);
$rss->items = array_slice($rss->items, 0, 1);
$channel = $rss->channel;
foreach ($rss->items as $item ) {
$parsed_url = parse_url(wp_filter_kses($item['link']));
echo wptexturize($item['description']);
echo "<p><a href=" . wp_filter_kses($item['link']) . ">" . wptexturize(wp_specialchars($item['author'])) . "</a></p>";
}
echo $before_after;
}
I'd like to decrease the default RSS cache time for a widget plugin i've made that outputs RSS feeds. I'm no php expert, I just copy and paste and understand little bits. I'm using this code to output the RSS
function widget($args, $instance) {
// outputs the content of the widget
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$tip_language = apply_filters('widget_title', $instance['tip_language']);
echo $before_widget;
echo "<h3 class=\"widgettitle\">" . $title . "</h3>";
include_once(ABSPATH . WPINC . '/rss.php');
$rss_feed = "http://mysite/feed";
$rss = fetch_rss($rss_feed);
$rss->items = array_slice($rss->items, 0, 1);
$channel = $rss->channel;
foreach ($rss->items as $item ) {
$parsed_url = parse_url(wp_filter_kses($item['link']));
echo wptexturize($item['description']);
echo "<p><a href=" . wp_filter_kses($item['link']) . ">" . wptexturize(wp_specialchars($item['author'])) . "</a></p>";
}
echo $before_after;
}
Share
Improve this question
asked Jan 8, 2011 at 5:54
agileapricotagileapricot
1,5194 gold badges19 silver badges28 bronze badges
2 Answers
Reset to default 5Why not just use native RSS widget?
Also while Chris_O is absolutely right on filter to use, it is better practice to change cache time for specific feed URL rather than globally:
add_filter( 'wp_feed_cache_transient_lifetime', 'speed_up_feed', 10, 2 );
function speed_up_feed( $interval, $url ) {
if( 'http://mysite/feed' == $url )
return 3600;
return $interval;
}
The default lifetime of a transient cached feed is 12 hours. It can be changed by hooking in to the wp_feed_cache_transient_lifetime filter.
This will change the feed cache to update every 10 minutes:
add_filter( 'wp_feed_cache_transient_lifetime',
create_function('$a', 'return 600;') );
Also the code you copied and pasted is pretty outdated. It contains deprecated functions and does not take advantage of the Widgets API. fetch_rss was replaced with fetch_feed. wp_specialchars()
was replaced with esc_html()
-> see WP Hackers Discussion.
The Widgets API is very well documented and with a few modifications your code could take advantage of the new features.
本文标签: Decrease RSS cache time in plugin
版权声明:本文标题:Decrease RSS cache time in plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744715183a2621360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论