Last active
December 14, 2015 13:29
-
-
Save gyrus/5094262 to your computer and use it in GitHub Desktop.
Generic WordPress caching with Transients API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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