media_upload_form_handler

Handles form submissions for the legacy media uploader.

Description

media_upload_form_handler(); 

Usage

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

Defined (1)

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

/wp-admin/includes/media.php  
  1. function media_upload_form_handler() { 
  2. check_admin_referer('media-form'); 
  3.  
  4. $errors = null; 
  5.  
  6. if ( isset($_POST['send']) ) { 
  7. $keys = array_keys( $_POST['send'] ); 
  8. $send_id = (int) reset( $keys ); 
  9.  
  10. if ( !empty($_POST['attachments']) ) foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { 
  11. $post = $_post = get_post($attachment_id, ARRAY_A); 
  12.  
  13. if ( !current_user_can( 'edit_post', $attachment_id ) ) 
  14. continue; 
  15.  
  16. if ( isset($attachment['post_content']) ) 
  17. $post['post_content'] = $attachment['post_content']; 
  18. if ( isset($attachment['post_title']) ) 
  19. $post['post_title'] = $attachment['post_title']; 
  20. if ( isset($attachment['post_excerpt']) ) 
  21. $post['post_excerpt'] = $attachment['post_excerpt']; 
  22. if ( isset($attachment['menu_order']) ) 
  23. $post['menu_order'] = $attachment['menu_order']; 
  24.  
  25. if ( isset($send_id) && $attachment_id == $send_id ) { 
  26. if ( isset($attachment['post_parent']) ) 
  27. $post['post_parent'] = $attachment['post_parent']; 
  28.  
  29. /** 
  30. * Filters the attachment fields to be saved. 
  31. * @since 2.5.0 
  32. * @param array $post An array of post data. 
  33. * @param array $attachment An array of attachment metadata. 
  34. */ 
  35. $post = apply_filters( 'attachment_fields_to_save', $post, $attachment ); 
  36.  
  37. if ( isset($attachment['image_alt']) ) { 
  38. $image_alt = wp_unslash( $attachment['image_alt'] ); 
  39. if ( $image_alt != get_post_meta($attachment_id, '_wp_attachment_image_alt', true) ) { 
  40. $image_alt = wp_strip_all_tags( $image_alt, true ); 
  41.  
  42. // Update_meta expects slashed. 
  43. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); 
  44.  
  45. if ( isset($post['errors']) ) { 
  46. $errors[$attachment_id] = $post['errors']; 
  47. unset($post['errors']); 
  48.  
  49. if ( $post != $_post ) 
  50. wp_update_post($post); 
  51.  
  52. foreach ( get_attachment_taxonomies($post) as $t ) { 
  53. if ( isset($attachment[$t]) ) 
  54. wp_set_object_terms($attachment_id, array_map('trim', preg_split('/, +/', $attachment[$t])), $t, false); 
  55.  
  56. if ( isset($_POST['insert-gallery']) || isset($_POST['update-gallery']) ) { ?> 
  57. <script type="text/javascript"> 
  58. var win = window.dialogArguments || opener || parent || top; 
  59. win.tb_remove(); 
  60. </script> 
  61. <?php 
  62. exit; 
  63.  
  64. if ( isset($send_id) ) { 
  65. $attachment = wp_unslash( $_POST['attachments'][$send_id] ); 
  66.  
  67. $html = isset( $attachment['post_title'] ) ? $attachment['post_title'] : ''; 
  68. if ( !empty($attachment['url']) ) { 
  69. $rel = ''; 
  70. if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link($send_id) == $attachment['url'] ) 
  71. $rel = " rel='attachment wp-att-" . esc_attr($send_id) . "'"; 
  72. $html = "<a href='{$attachment['url']}'$rel>$html</a>"; 
  73.  
  74. /** 
  75. * Filters the HTML markup for a media item sent to the editor. 
  76. * @since 2.5.0 
  77. * @param string $html HTML markup for a media item sent to the editor. 
  78. * @param int $send_id The first key from the $_POST['send'] data. 
  79. * @param array $attachment Array of attachment metadata. 
  80. */ 
  81. $html = apply_filters( 'media_send_to_editor', $html, $send_id, $attachment ); 
  82. return media_send_to_editor($html); 
  83.  
  84. return $errors;