{"id":4029,"date":"2014-03-19T12:07:15","date_gmt":"2014-03-19T11:07:15","guid":{"rendered":"http:\/\/tech4sky.com\/?p=4029"},"modified":"2014-03-19T13:01:15","modified_gmt":"2014-03-19T12:01:15","slug":"altering-wordpress-feed-cache-lifetime","status":"publish","type":"post","link":"https:\/\/w3guy.com\/altering-wordpress-feed-cache-lifetime\/","title":{"rendered":"Altering WordPress Feed&#8217;s Cache Lifetime with &#8216;wp_feed_cache_transient_lifetime&#8217; filter"},"content":{"rendered":"<p>It a well-known fact that when you publish an article not just in WordPress but also in most blogging platforms and CMS, the <a href=\"http:\/\/feeds.feedburner.com\/tech4sky\" target=\"_blank\" rel=\"noopener\">feed<\/a> get updated automatically with the article. But do you know editing an already published article doesn&#8217;t auto-update the feed? <\/p>\n<p>By default, WordPress cache feed or rather the cached feed has a lifetime of <em>43200 seconds<\/em>, equivalent to <em>12 hours<\/em>. There isn&#8217;t an option to purge the cached feed.<\/p>\n<p>Say, you published an article and later discovered a typographical error was made. You swiftly edited and corrected the mistake. But do you know that the correction didn&#8217;t reflect in your WordPress feed?<br \/>\nThe effect of this is, a reader who subscribed to your feed will receive and read the article containing the error because the feed will only get updated in 12 hours time.<\/p>\n<p>You can actually reduce or increase the default cache lifetime simply by hooking a function that returns an integer (time in seconds) into the <span class=\"spanz\"><code class=\"\" data-line=\"\">wp_feed_cache_transient_lifetime<\/code><\/span> filter.<\/p>\n<pre><code class=\"language-php\" data-line=\"\">\nfunction return_cache_time( $seconds )\n{\n\/\/ change the default feed cache recreation period to 2 hours\nreturn (int) 7200;\n}\n\n\/\/set feed cache duration\nadd_filter( &#039;wp_feed_cache_transient_lifetime&#039;, &#039;return_cache_time&#039;);\n<\/code><\/pre>\n<p>The above code snippet will cache the feed for <em>7200 seconds (2 hrs)<\/em>. setting it <em>0<\/em> actually might disable feed caching.<\/p>\n<p>To use the code, you might want to add it to your theme&#8217;s <code class=\"\" data-line=\"\">functions.php<\/code> or create a plugin with it.<\/p>\n<p>Also, say you are building a plugin that displays content from RSS feed using <a href=\"http:\/\/codex.wordpress.org\/Function_Reference\/fetch_feed\" target=\"_blank\" rel=\"noopener\">fetch_feed<\/a> function, you can set the cache lifetime by adding the <code class=\"\" data-line=\"\">wp_feed_cache_transient_lifetime<\/code> filter before the <code class=\"\" data-line=\"\">fetch_feed<\/code> code and afterward removing it like so:<\/p>\n<pre><code class=\"language-php\" data-line=\"\">\nfunction return_cache_time( $seconds )\n{\n\/\/ change the default feed cache recreation period to 2 hours\nreturn (int) 7200;\n}\n\n\/\/ adds the filter to set cache lifetime\nadd_filter( &#039;wp_feed_cache_transient_lifetime&#039; , &#039;return_7200&#039; );\n\n$feed = fetch_feed( $feed_url );\n\n\/\/ remove the filter to reset the cache lifetime to default value\nremove_filter( &#039;wp_feed_cache_transient_lifetime&#039; , &#039;return_cache_time&#039; );\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It a well-known fact that when you publish an article not just in WordPress but also in most blogging platforms and CMS, the feed get updated automatically with the article. But do you know editing an already published article doesn&#8217;t auto-update the feed? By default, WordPress cache feed or rather the cached feed has a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1351,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mo_disable_npp":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[],"class_list":["post-4029","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/w3guy.com\/wp-content\/uploads\/2013\/03\/WordPress-Theme-Things-To-Remember-After-A-Theme-Change.jpg","jetpack-related-posts":[{"id":3403,"url":"https:\/\/w3guy.com\/retrieving-content-feed-simplepie-php\/","url_meta":{"origin":4029,"position":0},"title":"PHP Tutorial &#8211; Retrieving Contents From Website Feed via SimplePie","author":"Collins Agbonghama","date":"September 23, 2013","format":false,"excerpt":"While i was writing my HTTP Header Checker tool, i decided to include latest blog post of this blog at the footer. i wanted a system to automatically retrieve my latest blog post, so i finally settled for SimplePie a fast, easy-to-use, RSS and Atom feed parsing in PHP to\u2026","rel":"","context":"In &quot;PHP Tutorials&quot;","block_context":{"text":"PHP Tutorials","link":"https:\/\/w3guy.com\/category\/webmaster\/php\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/w3guy.com\/wp-content\/uploads\/2013\/09\/tech4skyfeed.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4055,"url":"https:\/\/w3guy.com\/sqlite3-general-error-25-bind-column-index-range\/","url_meta":{"origin":4029,"position":1},"title":"Fix to SQLite 3 &#8211; General error: 25 bind or column index out of range","author":"Collins Agbonghama","date":"March 24, 2014","format":false,"excerpt":"Few days ago, I decided to build my own minimalistic Feed reader using SQLite3 for the backend and SimplePie - a very fast and easy-to-use feed parser, written in PHP. Note: I am using PDO as my database abstraction layer. I wrote the PHP class to connect to the SQLite\u2026","rel":"","context":"In &quot;PHP Tutorials&quot;","block_context":{"text":"PHP Tutorials","link":"https:\/\/w3guy.com\/category\/webmaster\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":995,"url":"https:\/\/w3guy.com\/special-google-search-query-seo-arsenal\/","url_meta":{"origin":4029,"position":2},"title":"Special Google Search Query Terms To Have In Your SEO Arsenal","author":"Collins Agbonghama","date":"February 10, 2013","format":false,"excerpt":"If you are a webmaster, blogger or web developer, there are some special google search query you need to know and also add to your SEO arsenal. Not only are they special, they are very important and handy when doing SEO researches and task. They are - info:yoursite.comsite:yoursite.comlink:yoursite.comcache:yoursite.comrelated:yoursite.com Below are\u2026","rel":"","context":"In &quot;SEO&quot;","block_context":{"text":"SEO","link":"https:\/\/w3guy.com\/category\/seo-sem\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/w3guy.com\/wp-content\/uploads\/2013\/02\/Special-google-searches.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":5776,"url":"https:\/\/w3guy.com\/amazon-cloudfront-https-ssl-error\/","url_meta":{"origin":4029,"position":3},"title":"Amazon CloudFront Not Working with Your HTTPS\/SSL Website? Read This","author":"Collins Agbonghama","date":"November 17, 2017","format":false,"excerpt":"Few years ago, I decided to use Amazon CloudFront CDN (content delivery network) on this blog to boost it loading speed which has HTTPS\/SSL provided and powered by Cloudflare flexible SSL. After the setup with origin set to the domain name, I then rewrote all images, fonts, CSS and JavaScript\u2026","rel":"","context":"In &quot;Computer &amp; Internet&quot;","block_context":{"text":"Computer &amp; Internet","link":"https:\/\/w3guy.com\/category\/computer-internet\/"},"img":{"alt_text":"Amazon Cloudfront","src":"https:\/\/i0.wp.com\/w3guy.com\/wp-content\/uploads\/2017\/11\/amazon-cloudfront.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3617,"url":"https:\/\/w3guy.com\/fatal-error-call-member-function-fetch_object-non-object\/","url_meta":{"origin":4029,"position":4},"title":"How To Fix &#8211; Fatal error: Call to a member function fetch_object() on a non-object","author":"Collins Agbonghama","date":"November 14, 2013","format":false,"excerpt":"When coding, it is always advisable you check code to make sure the syntax are correct as majority of bugs arises from it. If you are using an Integrated development environment (IDE) such as NetBeans, Dreamweaver or Aptana Studio, you are notified when there is (syntax) error in your code.\u2026","rel":"","context":"In &quot;PHP Tutorials&quot;","block_context":{"text":"PHP Tutorials","link":"https:\/\/w3guy.com\/category\/webmaster\/php\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3522,"url":"https:\/\/w3guy.com\/wordpress-fix-pclzip-err-bad-format-10\/","url_meta":{"origin":4029,"position":5},"title":"How To Fix WordPress &#8211; PCLZIP_ERR_BAD_FORMAT (-10)","author":"Collins Agbonghama","date":"October 26, 2013","format":false,"excerpt":"\"Oh no... Not again, another WordPress ache\" That has often been my expression of late as a result of my frequent WordPress troubleshooting. Due to my busy schedule, I haven't had time to login to my blog. On one of my spare time, I took it as an opportunity to\u2026","rel":"","context":"In &quot;WordPress&quot;","block_context":{"text":"WordPress","link":"https:\/\/w3guy.com\/category\/wordpress\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/posts\/4029","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/comments?post=4029"}],"version-history":[{"count":0,"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/posts\/4029\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/media\/1351"}],"wp:attachment":[{"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/media?parent=4029"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/categories?post=4029"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3guy.com\/wp-json\/wp\/v2\/tags?post=4029"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}