Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. <?php
  2. /**
  3. * Upload Article Feature image.
  4. *
  5. * @param int $post_id Article ID.
  6. *
  7. * @param string $image_name image name.
  8. *
  9. * @param string $full_image_path Full image path from where image will upload.
  10. */
  11. function ie_upload_feature_image( $post_id, $image_name = '', $full_image_path = '' ) {
  12. if ( ! empty( $post_id ) && ! empty( $image_name ) && ! empty( $full_image_path ) ) {
  13. $upload = wp_upload_bits( $image_name, null, wpcom_vip_file_get_contents( esc_url_raw( $full_image_path ), 3, 60 ) );
  14. if ( is_array( $upload ) && array_key_exists( 'file', $upload ) ) {
  15. $filename = $upload['file'];
  16. $file_type = wp_check_filetype( $filename, null ); // Retrieve the file type from the file name.
  17. if ( is_array( $file_type ) && array_key_exists( 'type', $file_type ) ) {
  18. $attachment = array(
  19. 'post_mime_type' => $file_type['type'],
  20. 'post_title' => sanitize_file_name( $filename ),
  21. 'post_content' => '',
  22. 'post_status' => 'inherit',
  23. );
  24. $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
  25. if ( ! empty( $attach_id ) ) {
  26. require_once trailingslashit( ABSPATH ) . 'wp-admin/includes/image.php';
  27. $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
  28. wp_update_attachment_metadata( $attach_id, $attach_data );
  29. set_post_thumbnail( $post_id, $attach_id );
  30. }
  31. }
  32. }
  33. }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement