0

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?

1 Answer 1

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; }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.