# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Performance](https://wpide.com/library/category/performance/)
- Tags: caching, feeds, sitemaps
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/perf-feed-sitemap-cache-headers/

## Code

```php
// 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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Browser and CDN Caching for Feeds and Sitemaps" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
