Guest User

Untitled

a guest
Jan 16th, 2019
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. $username = "ZX";
  2. $password = "ZX";
  3. $host = 'http://ZX.com/wp-json/wp/v2/media';
  4. $data = json_encode($data);
  5. $file = '/Users/xx.png';
  6. $imagedata = file_get_contents($file);
  7. $process = curl_init($host);
  8. curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
  9. curl_setopt($process, CURLOPT_BINARYTRANSFER, TRUE);
  10. curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
  11. curl_setopt($process, CURLOPT_TIMEOUT, 50);
  12. curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
  13. curl_setopt($process, CURLOPT_POSTFIELDS, $data);
  14. curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type:image/png','Content-Disposition:attachment;filename='.$file));
  15. $return = curl_exec($process);
  16. print_r($return);
  17.  
  18. $data = array(
  19. "status" => "draft",
  20. "title" => "Photo media",
  21. "description" => "Photo media1",
  22. "media_type" => "image",
  23. "alt_text" => "alternate text"
  24. );
  25.  
  26. $media = media_sideload_image(
  27. 'http://i.imgur.com/bcJvAj0.jpg',
  28. $post->ID,
  29. 'Some image description',
  30. 'src'
  31. );
  32.  
  33. if ( ! empty( $media ) and ! is_wp_error( $media ) ) {
  34. // reference new image to set as featured
  35. $attachments = get_posts( [
  36. 'post_type' => 'attachment',
  37. 'posts_per_page' => 1,
  38. 'post_status' => 'any',
  39. 'post_parent' => $post->ID,
  40. ] );
  41.  
  42. if ( is_array( $attachments ) ) {
  43. set_post_thumbnail( $post->ID, $attachments[0]->ID );
  44. }
  45.  
  46. // Test print our image. The return value is the src of the sideloaded image.
  47. printf( '<img src="%s" />', $media );
  48. }
Add Comment
Please, Sign In to add comment