Skip to content

Instantly share code, notes, and snippets.

@gyrus
Last active December 14, 2015 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyrus/5094262 to your computer and use it in GitHub Desktop.
Save gyrus/5094262 to your computer and use it in GitHub Desktop.
Generic WordPress caching with Transients API
<?php
/**
* Get data with caching
*
* Change function name, transient name, and cache timeout if necessary.
* Populate $my_data, obviously.
* "refresh" query parameter useful for manual refreshing.
*/
function pilau_get_my_data() {
if ( isset( $_GET['refresh'] ) || false === ( $my_data = get_transient( 'pilau_my_data' ) ) ) {
$my_data = array();
set_transient( 'pilau_my_data', $my_data, 60*60*24 ); // Cache for 24 hours
}
return $my_data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment