wp_ajax_upload_attachment

Ajax handler for uploading attachments.

Description

wp_ajax_upload_attachment(); 

Usage

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

Defined (1)

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

/wp-admin/includes/ajax-actions.php  
  1. function wp_ajax_upload_attachment() { 
  2. check_ajax_referer( 'media-form' ); 
  3. /** 
  4. * This function does not use wp_send_json_success() / wp_send_json_error() 
  5. * as the html4 Plupload handler requires a text/html content-type for older IE. 
  6. * See https://core.trac.wordpress.org/ticket/31037 
  7. */ 
  8.  
  9. if ( ! current_user_can( 'upload_files' ) ) { 
  10. echo wp_json_encode( array( 
  11. 'success' => false,  
  12. 'data' => array( 
  13. 'message' => __( 'Sorry, you are not allowed to upload files.' ),  
  14. 'filename' => $_FILES['async-upload']['name'],  
  15. ) ); 
  16.  
  17. wp_die(); 
  18.  
  19. if ( isset( $_REQUEST['post_id'] ) ) { 
  20. $post_id = $_REQUEST['post_id']; 
  21. if ( ! current_user_can( 'edit_post', $post_id ) ) { 
  22. echo wp_json_encode( array( 
  23. 'success' => false,  
  24. 'data' => array( 
  25. 'message' => __( 'Sorry, you are not allowed to attach files to this post.' ),  
  26. 'filename' => $_FILES['async-upload']['name'],  
  27. ) ); 
  28.  
  29. wp_die(); 
  30. } else { 
  31. $post_id = null; 
  32.  
  33. $post_data = isset( $_REQUEST['post_data'] ) ? $_REQUEST['post_data'] : array(); 
  34.  
  35. // If the context is custom header or background, make sure the uploaded file is an image. 
  36. if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ) ) ) { 
  37. $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'] ); 
  38. if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { 
  39. echo wp_json_encode( array( 
  40. 'success' => false,  
  41. 'data' => array( 
  42. 'message' => __( 'The uploaded file is not a valid image. Please try again.' ),  
  43. 'filename' => $_FILES['async-upload']['name'],  
  44. ) ); 
  45.  
  46. wp_die(); 
  47.  
  48. $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data ); 
  49.  
  50. if ( is_wp_error( $attachment_id ) ) { 
  51. echo wp_json_encode( array( 
  52. 'success' => false,  
  53. 'data' => array( 
  54. 'message' => $attachment_id->get_error_message(),  
  55. 'filename' => $_FILES['async-upload']['name'],  
  56. ) ); 
  57.  
  58. wp_die(); 
  59.  
  60. if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) { 
  61. if ( 'custom-background' === $post_data['context'] ) 
  62. update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', $post_data['theme'] ); 
  63.  
  64. if ( 'custom-header' === $post_data['context'] ) 
  65. update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', $post_data['theme'] ); 
  66.  
  67. if ( ! $attachment = wp_prepare_attachment_for_js( $attachment_id ) ) 
  68. wp_die(); 
  69.  
  70. echo wp_json_encode( array( 
  71. 'success' => true,  
  72. 'data' => $attachment,  
  73. ) ); 
  74.  
  75. wp_die();