admin管理员组

文章数量:1134246

I'm trying to bypass the cache used by fetch_feed, for specific users to force an update, by looking for the transient and deleting it before calling fetch_feed.

    if ( $this->bypass ) {
      $identifier = md5($this->feedUrl);
      $cacheKey = 'feed_' . $identifier;
      $cachedResult = get_transient($cacheKey);
      delete_transient($cacheKey);
    }

    $feedData = fetch_feed($this->feedUrl);

This however doesn't generate the correct transient key. They key in the database will be feed_2e798d9381cdbac2bb90151bf553c129 but this code is giving me feed_3f88b9c0e4a4ed755f0216b12b320fa5.

Is there a way to get the correct key from the url, or is there a method to delete the transient key via url?

I'm trying to bypass the cache used by fetch_feed, for specific users to force an update, by looking for the transient and deleting it before calling fetch_feed.

    if ( $this->bypass ) {
      $identifier = md5($this->feedUrl);
      $cacheKey = 'feed_' . $identifier;
      $cachedResult = get_transient($cacheKey);
      delete_transient($cacheKey);
    }

    $feedData = fetch_feed($this->feedUrl);

This however doesn't generate the correct transient key. They key in the database will be feed_2e798d9381cdbac2bb90151bf553c129 but this code is giving me feed_3f88b9c0e4a4ed755f0216b12b320fa5.

Is there a way to get the correct key from the url, or is there a method to delete the transient key via url?

Share Improve this question edited Jul 30, 2023 at 20:45 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Jul 30, 2023 at 20:38 Picard102Picard102 9331 gold badge10 silver badges23 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

SimplePie does not necessarily use the exact feed URL that was passed to fetch_feed() – see SimplePie::get_cache_filename(), but yes, WordPress adds the feed_ prefix to the transient key.

So you can instead try using the wp_feed_options hook to get the correct transient key and then delete the existing transient.

$bypass = $this->bypass;

add_action(
    'wp_feed_options',
    function ( $feed ) use ( &$bypass ) {
        if ( $bypass ) {
            $cache_key = 'feed_' . $feed->get_cache_filename( $feed->feed_url );
            delete_transient( $cache_key );
            $bypass = false; // transient deleted, so reset this to the default
        }
    }
);

$feedData = fetch_feed( $this->feedUrl );

If you do not need to update the cached data, i.e. you just wanted to bypass reading from the cache, then you could simply use function ( $feed ) use ( &$bypass ) { $feed->cache = ! $bypass; }.

本文标签: feedBypass fetchfeed cache