wp_import_handle_upload

Handle importer uploading and add attachment.

Description

(array) wp_import_handle_upload(); 

Returns (array)

Uploaded file's details on success, error message on failure


Usage

  1. if ( !function_exists( 'wp_import_handle_upload' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/import.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_import_handle_upload(); 
  7.  

Defined (1)

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

/wp-admin/includes/import.php  
  1. function wp_import_handle_upload() { 
  2. if ( ! isset( $_FILES['import'] ) ) { 
  3. return array( 
  4. error => __( 'File is empty. Please upload something more substantial. Thiserrorcould also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' ) 
  5. ); 
  6.  
  7. $overrides = array( 'test_form' => false, 'test_type' => false ); 
  8. $_FILES['import']['name'] .= '.txt'; 
  9. $upload = wp_handle_upload( $_FILES['import'], $overrides ); 
  10.  
  11. if ( isset( $upload[error] ) ) { 
  12. return $upload; 
  13.  
  14. // Construct the object array 
  15. $object = array( 
  16. 'post_title' => basename( $upload['file'] ),  
  17. 'post_content' => $upload['url'],  
  18. 'post_mime_type' => $upload['type'],  
  19. 'guid' => $upload['url'],  
  20. 'context' => 'import',  
  21. 'post_status' => 'private' 
  22. ); 
  23.  
  24. // Save the data 
  25. $id = wp_insert_attachment( $object, $upload['file'] ); 
  26.  
  27. /** 
  28. * Schedule a cleanup for one day from now in case of failed 
  29. * import or missing wp_import_cleanup() call. 
  30. */ 
  31. wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) ); 
  32.  
  33. return array( 'file' => $upload['file'], 'id' => $id );