How to change the update time (cache time) of RSS feed block

How to change the update time (cache time) of RSS feed block

Thank you for visiting. This page is an English translation of the Japanese site [Knowledge Base] using Google Translate. Please understand that there are some points that are difficult to read, such as sentences and expressions.

In WordPress, when receiving and listing RSS feeds of external sites using the “RSS” block of the block editor, it is updated every 12 hours (12 hours are not reacquired). (This is called a feed cache).

This is set as standard as a measure to reduce the processing of the page and the load on the feed source site by receiving the feed and displaying it as a list every time the page with the “RSS” block etc. inserted is displayed. It has been.

However, depending on the site or page, you may want to always receive and display the latest information.

In such a case, you can change the feed cache time arbitrarily by adding the following code to the theme’s functions.php and specifying the time.

7200 in the code means 7200 seconds (120 minutes = 2 hours), so change it to any number according to the purpose.

/********** RSSフィードのキャッシュ時間変更 **********/
 /*** rerurnの値を変更することで時間変更可能(秒で指定)***/
 function ha_feedcachetime( $seconds ) {
    return 7200;
 }
 add_filter('wp_feed_cache_transient_lifetime','ha_feedcachetime');
 $feed = fetch_feed( $feed_url );
 remove_filter('wp_feed_cache_transient_lifetime','ha_feedcachetime');

Also, if you want to make it completely on-demand (no cache), just add the following code to functions.php of the same theme.

/***** フィードのキャッシュをオフにする *****/
function ha_turn_off_feed_caching($feed){
	$feed-> enable_cache(false);
}
add_action('wp_feed_options','ha_turn_off_feed_caching');

Regardless of which method you use, if you have multiple feed receiving lists on your page, or if you want to view feeds from multiple sites together, the server will be overloaded and your page will slow down. Please check carefully before using.

Reference article (WordPress Codex)

Post Author: