Skip to content

Instantly share code, notes, and snippets.

@debonx
Created November 2, 2018 17:49
Show Gist options
  • Save debonx/3a93726313decd2efaabc5f92849343c to your computer and use it in GitHub Desktop.
Save debonx/3a93726313decd2efaabc5f92849343c to your computer and use it in GitHub Desktop.
How to update and check transients on Wordpress. More at https://xilab.co.
/*
* @param $tag = tag to use for this transient
* @param $element = value to associate the transient
* @param $duration / $expiration = time in seconds > 3600*24 for 1 day
* @return true > if needed to update | false > if still not expired
*/
function update_transient($tag, $element, $duration, $expiration){
$elements_in_tag = get_transient($tag);
$need_to_update = isset($elements_in_tag[$element]) && $elements_in_tag[$element] > (time() - ($duration));
if($need_to_update){
$elements_in_tag[$element] = time();
set_transient($tag, $elements_in_tag, $expiration);
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment