Get Remote Part — a handy theme function

Handy little code snippet if your theme or plugin ever needs to regularly check a remote url’s contents, but you want to cache it for a bit:

function get_remote_part( $url, $minutes_to_save = 60 ) {
	$transient_name = 'get_remote_part_' . substr( md5( $url ), 16 );
	if ( false === ( $value = get_transient( $transient_name ) ) ) {
		$value = wp_remote_retrieve_body( wp_remote_get( $url ) );
		if( $value ) {
			set_transient( $transient_name, $value, ( MINUTE_IN_SECONDS * $minutes_to_save ) );
		}
	}
	return $value;
}

Author: George Stephanis

Cooking, Code, Carpentry, Letterpress.