lolitaloco

inser Instagram post as WordPress post

Jan 20th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1.             /* FETCH */
  2.             $content = file_get_contents("https://api.instagram.com/v1/users/self/media/recent/?access_token=$accesstoken&count=$count");  
  3.             $feed = json_decode($content,true);
  4.            
  5.             foreach($feed[data] as $image) {
  6.                
  7.                 $id = $image[id];  
  8.                 $thumb = $image[images][thumbnail][url];
  9.                 $fullImage = $image[images][standard_resolution][url]; 
  10.                 $link = $image[link];      
  11.                 $tags = $image[tags];                  
  12.                 $caption = $image[caption][text];
  13.                
  14.                 $postExists = get_posts(array(
  15.                     "meta_key" => "instagram_id",
  16.                     "meta_value" => $id,
  17.                     "post_status" => true
  18.                 ));
  19.                
  20.                 /* If no posts with this instagram_id exists (with any post status), add it: */
  21.                 if(empty($postExists)){                    
  22.                    
  23.                     // new post arguments
  24.                         $post = array(
  25.                             "post_title" => "Instagram " . $id,
  26.                             "post_category" => array(get_cat_ID($category)),
  27.                             "post_content" => "<p>$caption</p>",
  28.                             "post_status" => "publish",
  29.                             "post_type" => "post"
  30.                         );
  31.                    
  32.                     // adding the new post
  33.                     $post_id = wp_insert_post($post);
  34.                     update_post_meta($post_id,'instagram_id',$id);
  35.                     wp_set_post_terms($post_id, $tags);
  36.                        
  37.                     // adding the image as attachment
  38.                     require_once(ABSPATH . 'wp-admin/includes/file.php');
  39.                     require_once(ABSPATH . 'wp-admin/includes/media.php');
  40.                     require_once(ABSPATH . 'wp-admin/includes/image.php');                 
  41.                     media_sideload_image($fullImage, $post_id, "Instagram");       
  42.                    
  43.                 }
  44.             }
Add Comment
Please, Sign In to add comment