Skip to content

Instantly share code, notes, and snippets.

@epierpont
Last active August 29, 2015 14:16
Show Gist options
  • Save epierpont/683d63975160341563fa to your computer and use it in GitHub Desktop.
Save epierpont/683d63975160341563fa to your computer and use it in GitHub Desktop.
WP - Get remote json file and cache
<?php
// using http://codex.wordpress.org/Function_Reference/wp_remote_get and http://codex.wordpress.org/Transients_API
// if transient has expired then recreate, if not grab from db
if ( false === ( $featured_trans = get_transient( 'featured_trans' ) ) ) {
$featured_args = array(
'timeout' => 5,
'redirection' => 5,
'sslverify' => false
);
$featured_feed = wp_remote_get( 'http://www.example.com/somefeed/', $featured_args );
$featured_json = $featured_feed['body'];
$featured_array = json_decode( $featured_json, true );
$featured_trans = $featured_array;
set_transient( 'featured_trans', $featured_trans, 12 * HOUR_IN_SECONDS ); // expiration is set for 12 hours
}
// do something with $featured_trans
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment