Created
July 31, 2018 21:35
-
-
Save twentyfortysix/fd3a1e8559850c4c72edcbd6ebad4019 to your computer and use it in GitHub Desktop.
WP - get and cache json
This file contains hidden or 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
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