media_handle_sideload

Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().

Description

(int|object) media_handle_sideload( (array) $file_array, (int) $post_id, (constant) $desc = null, (array) $post_data = array() ); 

Returns (int|object)

The ID of the attachment or a WP_Error on failure.

Parameters (4)

0. $file_array (array)
Array similar to a $_FILES upload array.
1. $post_id (int)
The post ID the media is associated with.
2. $desc — Optional. (constant) => null
Description of the side-loaded file. Default null.
3. $post_data — Optional. (array) => array()
Post data to override. Default empty array.

Usage

  1. if ( !function_exists( 'media_handle_sideload' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/media.php'; 
  3.  
  4. // Array similar to a `$_FILES` upload array. 
  5. $file_array = array(); 
  6.  
  7. // The post ID the media is associated with. 
  8. $post_id = -1; 
  9.  
  10. // Optional. Description of the side-loaded file. Default null. 
  11. $desc = null; 
  12.  
  13. // Optional. Post data to override. Default empty array. 
  14. $post_data = array(); 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = media_handle_sideload($file_array, $post_id, $desc, $post_data); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/media.php  
  1. function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data = array() ) { 
  2. $overrides = array('test_form'=>false); 
  3.  
  4. $time = current_time( 'mysql' ); 
  5. if ( $post = get_post( $post_id ) ) { 
  6. if ( substr( $post->post_date, 0, 4 ) > 0 ) 
  7. $time = $post->post_date; 
  8.  
  9. $file = wp_handle_sideload( $file_array, $overrides, $time ); 
  10. if ( isset($file[error]) ) 
  11. return new WP_Error( 'upload_error', $file[error] ); 
  12.  
  13. $url = $file['url']; 
  14. $type = $file['type']; 
  15. $file = $file['file']; 
  16. $title = preg_replace('/\.[^.]+$/', '', basename($file)); 
  17. $content = ''; 
  18.  
  19. // Use image exif/iptc data for title and caption defaults if possible. 
  20. if ( $image_meta = @wp_read_image_metadata($file) ) { 
  21. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) 
  22. $title = $image_meta['title']; 
  23. if ( trim( $image_meta['caption'] ) ) 
  24. $content = $image_meta['caption']; 
  25.  
  26. if ( isset( $desc ) ) 
  27. $title = $desc; 
  28.  
  29. // Construct the attachment array. 
  30. $attachment = array_merge( array( 
  31. 'post_mime_type' => $type,  
  32. 'guid' => $url,  
  33. 'post_parent' => $post_id,  
  34. 'post_title' => $title,  
  35. 'post_content' => $content,  
  36. ), $post_data ); 
  37.  
  38. // This should never be set as it would then overwrite an existing attachment. 
  39. unset( $attachment['ID'] ); 
  40.  
  41. // Save the attachment metadata 
  42. $id = wp_insert_attachment($attachment, $file, $post_id); 
  43. if ( !is_wp_error($id) ) 
  44.  
  45. return $id;