Skip to content

Instantly share code, notes, and snippets.

@johnmontfx
Last active January 26, 2016 20:28
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 johnmontfx/3fc8899b28e95fadfb54 to your computer and use it in GitHub Desktop.
Save johnmontfx/3fc8899b28e95fadfb54 to your computer and use it in GitHub Desktop.
A quick example of transients
// set the variable $instagram_data to the data previously stored as a transient.
// if the transient has expired or there is not data, the variable will be empty.
$instagram_data = get_transient('myprefix_instagram_data');
// if the variable is empty, we need to get the new data from instagram
// But if it is not empty, it will skip all the steps inside this loop, saving time.
if ( !$instagram_data ) {
// Do stuff here to get instagram information
// This is where your site contacts instagram to get information on reent photos
// Generally something that takes time tdo
$new_data = {DO STUFF HERE};
// OK, so now we have our data that we can use in our script.
// But before we use it, we'll save the transient for later use
//
// This sets the transient, which is stored in the WordPress database or in object cache if yuou have
// it turned on for your web site (can happen through caching plugins and web config)
//
// The parts to the data are:
// 'myprefix_instagram_data' - the name of the transient stored
// $instagram_data - the data gotten in the DO STUFF HERE section
// '60' - the number of seconds that the transient will be valid until it expires.
//
// To figure out the number of seconds, I just google "Convert 5 hours to seconds"
// This is set to three hours (10800 seconds)
set_transient( 'myprefix_instagram_data', $new_data, '10800' );
// Now let's set the variable $instagram_data to the new data we just got
$instagram_data = $new_data;
}
// Do the rest of your stuff here....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment