[WordPress] wp_remote_get の結果を transient API でキャッシュしておく奴

<?php
function transient_remote_get($url, $expiration = 3600) {
$transient = 'remote_get-' . md5($url);
if ( ! ($response_body = get_transient($transient)) ) {
$response = wp_remote_get($url);
if( !is_wp_error($response) && $response["response"]["code"] === 200 ) {
$response_body = $response["body"];
set_transient($transient, $response_body, $expiration);
} else {
$response_body = false;
}
}
return $response_body;
}

view raw
gistfile1.php
hosted with ❤ by GitHub