Home / WordPress / Filtering transient expiration time in WordPress

Filtering transient expiration time in WordPress

As of now WordPress doesn’t offer a filter to control the expiration of the transient while it is being set, so I quickly wanted to share a quick snippet I found on Trac, written by Andrew Nacin.

Here is the snippet:

View this code snippet on GitHub.

Basically, 'set_transient_' . $transient hook is fired once the transient gets saved, so it saves itself again resulting in recursion as the same hook will be fired again, but before it saves itself again, it checks whether the $desired_expiration is different from the $expiration with which it’s being saved. Smart!

On a side note, if you ever need to do this, don’t use anonymous functions because that takes away the ability to unhook your function by someone else. It is just a quick way to demonstrate the idea.

Leave a Reply

Your email address will not be published. Required fields are marked *