简体   繁体   中英

Adding photos from instagram to media library wordpress

I have created the following php file to get the instagram photos for a specific user. Can anyone point me in the right direction as to how I can then add these photos to the media library in my wordpress site?

<?php
/**
 * Template Name: Cronjob
 */

  function fetchData($url){
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $url);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 20);
      $result = curl_exec($ch);
      curl_close($ch); 
      return $result;
  }

  $result = fetchData("https://api.instagram.com/v1/users/******/media/recent/?access_token=*************&count=5");
  $result = json_decode($result);
  foreach ($result->data as $post) {

    $new_post = wp_insert_post( array(
        'post_content'  => '<a href="'. esc_url( $post->link ) .'" target="_blank"><img src="'. esc_url( $post->images->standard_resolution->url ) .'" alt="'. $post->caption->text .'" /></a>',
        'post_name'     => $post->id,
        'post_type' => 'instagram'
    ), true );
  }
?>

Have you tried using Instagram Importer-Plugin ?

Should do the job for you! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM