Menu Close

Filtering transient expiration time in WordPress

As of now WordPress, doesn’t offer a filter to control the expiration of the transient while its 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. Its just a quick way to demonstrate the idea.

Concerned Trac Ticket – https://core.trac.wordpress.org/ticket/21330

The post Filtering transient expiration time in WordPress appeared first on Ashfame | Blog.

Leave a Reply

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