Created
          February 5, 2014 19:41 
        
      - 
      
- 
        Save drrobotnik/8831455 to your computer and use it in GitHub Desktop. 
    remote_get
  
        
  
    
      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 remote_get($key, $url, $args = array(), $expiration = 604800 ){ | |
| if( empty( $expiration ) ){ | |
| $results = get_option( $key ); | |
| }else{ | |
| $results = get_transient( $key ); | |
| } | |
| if ( false === $results ) { | |
| $response = wp_remote_get($url, $args); | |
| $status = wp_remote_retrieve_response_code( $response ); | |
| if($status == 200){ | |
| $body = wp_remote_retrieve_body( $response ); | |
| if(!$expiration){ | |
| update_option( $key, $body ); | |
| }else{ | |
| set_transient($key, $body, $expiration); | |
| } | |
| return $body; | |
| }else{ | |
| return wp_remote_retrieve_response_message( $response ); | |
| } | |
| } | |
| return $results; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment