wp_feed_cache_transient_lifetime
云策文档标注
概述
wp_feed_cache_transient_lifetime 是一个 WordPress 过滤器,用于修改 RSS 或 Atom 源缓存的 transient 生命周期。它允许开发者自定义缓存持续时间,默认值为 12 小时。
关键要点
- 过滤器名称:wp_feed_cache_transient_lifetime
- 参数:$lifetime(缓存持续时间,单位为秒,默认 43200 秒)和 $name(缓存对象的唯一标识符)
- 应用场景:在 WP_Feed_Cache_Transient::__construct() 和 fetch_feed() 中使用,控制 feed 缓存更新频率
- 版本历史:自 WordPress 2.8.0 引入
代码示例
add_filter( 'wp_feed_cache_transient_lifetime' , 'return_7200' );
$feed = fetch_feed( $feed_url );
remove_filter( 'wp_feed_cache_transient_lifetime' , 'return_7200' );
function return_7200( $seconds ) {
// change the default feed cache recreation period to 2 hours
return 7200;
}注意事项
使用此过滤器时,建议在特定 feed 请求前后添加和移除过滤器,以避免影响其他缓存操作。示例中展示了如何临时将缓存时间改为 2 小时。
原文内容
Filters the transient lifetime of the feed cache.
Parameters
$lifetimeint-
Cache duration in seconds. Default is 43200 seconds (12 hours).
$namestring-
Unique identifier for the cache object.
Source
$this->lifetime = apply_filters( 'wp_feed_cache_transient_lifetime', $lifetime, $name );
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
Skip to note 2 content
Steven Lin
Example Migrated from Codex:
add_filter( 'wp_feed_cache_transient_lifetime' , 'return_7200' ); $feed = fetch_feed( $feed_url ); remove_filter( 'wp_feed_cache_transient_lifetime' , 'return_7200' ); function return_7200( $seconds ) { // change the default feed cache recreation period to 2 hours return 7200; }