Feeds are very useful features to read contents.So, Today I am going to explain about how to use the feed in WordPress and which functions WordPress provides for feeds.
As you all know WordPress provides the feature for reading feeds and using it in your blog.Wordpress has function fetch_feed() to work with feeds in WordPress and you can cache feed data using this function.
The fetch_feed() function checks the feed in every 12hours so data will be automatically deleted after 12 hours.fetch_feed() function cache data into transients by WordPress transients API.so you can get cached data transients into table wp_options.
SYNTAX
1 2 3 | $feed = fetch_feed( $uri ); |
fetch_feed() function call with the URL of the feed and returns a feed object that we store in the $feed.
Basically, fetch_feed() uses SimplePie
library of core WordPress is a simple way for fetching a feed. SimplePie is stable user-friendly class and documentation and ease of use is awesome, even if the functionality to retrieve the feed looks more complicated on this end.
1 2 3 4 5 6 7 8 9 10 | $feed = fetch_feed( $url ); if ( is_wp_error($feed) ) { $feed->enable_cache(true); $feed->set_cache_duration($cache_duration); } return $feed; |
Above small example reads the feed and if no error enables the cache and set its duration.Then You can use functions like get_items(), get_permalink(), get_title() to grab data from the feed and display on the screen.WordPress caches feed data using transient api
into database and clear every 12 hours so
To clear cache forcefully,use filter into functions.php or plugin file
1 2 3 | add_filter( 'wp_feed_cache_transient_lifetime', create_function('$a', 'return 0;') ); |
And if you don’t want to cache feed, set false in enable_cache
1 2 3 | $feed->enable_cache(false); |
Conclusion
The fetch_feed() function is the simplest way to customize or update your WordPress blog.Nowadays every site has feeds so you can easily scrape content and publish in your website.
Suggested Reading:
Parsing an RSS Feed with WordPress
Add items to top navigation in WordPress
To Create own directory in WordPress Upload folder