Browser and CDN Caching for Feeds and Sitemaps
Sends cache headers on RSS feeds and XML sitemaps so feed readers, crawlers and CDNs can reuse them for an hour instead of making WordPress rebuild them on every request.
Opens WPIDE on
Code
// Seconds feeds and sitemaps may be cached.
$wpide_feed_cache_seconds = HOUR_IN_SECONDS;
add_action( 'template_redirect', function () use ( $wpide_feed_cache_seconds ) {
if ( headers_sent() || is_user_logged_in() ) {
return;
}
if ( is_feed() || get_query_var( 'sitemap' ) || get_query_var( 'sitemap-stylesheet' ) ) {
header( sprintf( 'Cache-Control: public, max-age=%1$d, s-maxage=%1$d, stale-while-revalidate=60', (int) $wpide_feed_cache_seconds ) );
}
}, 1 );
Notes
Lower $wpide_feed_cache_seconds if you publish many times an hour and want new posts to appear in feeds faster.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.