Skip to content

Instantly share code, notes, and snippets.

@twentyfortysix
Created July 31, 2018 21:35
Show Gist options
  • Save twentyfortysix/fd3a1e8559850c4c72edcbd6ebad4019 to your computer and use it in GitHub Desktop.
Save twentyfortysix/fd3a1e8559850c4c72edcbd6ebad4019 to your computer and use it in GitHub Desktop.
WP - get and cache json
function get_json($url,$id){
$_url = $url.$id;
$transient = get_transient( $_url );
if(!empty($transient)){
return $transient;
}else{
$request = wp_remote_get( $_url );
if( is_wp_error( $request ) ) {
return false; // Bail early
}else{
$body = wp_remote_retrieve_body( $request );
$data = json_decode( $body , true);
set_transient( $_url, $data, $expiration = 7200 );
return $data;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment