wp_media_upload_handler

Handles the process of uploading media.

Description

(null|string) wp_media_upload_handler(); 

Returns (null|string)


Usage

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

Defined (1)

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

/wp-admin/includes/media.php  
  1. function wp_media_upload_handler() { 
  2. $errors = array(); 
  3. $id = 0; 
  4.  
  5. if ( isset($_POST['html-upload']) && !empty($_FILES) ) { 
  6. check_admin_referer('media-form'); 
  7. // Upload File button was clicked 
  8. $id = media_handle_upload('async-upload', $_REQUEST['post_id']); 
  9. unset($_FILES); 
  10. if ( is_wp_error($id) ) { 
  11. $errors['upload_error'] = $id; 
  12. $id = false; 
  13.  
  14. if ( !empty($_POST['insertonlybutton']) ) { 
  15. $src = $_POST['src']; 
  16. if ( !empty($src) && !strpos($src, '://') ) 
  17. $src = "http://$src"; 
  18.  
  19. if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) { 
  20. $title = esc_html( wp_unslash( $_POST['title'] ) ); 
  21. if ( empty( $title ) ) 
  22. $title = esc_html( basename( $src ) ); 
  23.  
  24. if ( $title && $src ) 
  25. $html = "<a href='" . esc_url($src) . "'>$title</a>"; 
  26.  
  27. $type = 'file'; 
  28. if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) ) 
  29. && ( 'audio' == $ext_type || 'video' == $ext_type ) ) 
  30. $type = $ext_type; 
  31.  
  32. /** 
  33. * Filters the URL sent to the editor for a specific media type. 
  34. * The dynamic portion of the hook name, `$type`, refers to the type 
  35. * of media being sent. 
  36. * @since 3.3.0 
  37. * @param string $html HTML markup sent to the editor. 
  38. * @param string $src Media source URL. 
  39. * @param string $title Media title. 
  40. */ 
  41. $html = apply_filters( "{$type}_send_to_editor_url", $html, esc_url_raw( $src ), $title ); 
  42. } else { 
  43. $align = ''; 
  44. $alt = esc_attr( wp_unslash( $_POST['alt'] ) ); 
  45. if ( isset($_POST['align']) ) { 
  46. $align = esc_attr( wp_unslash( $_POST['align'] ) ); 
  47. $class = " class='align$align'"; 
  48. if ( !empty($src) ) 
  49. $html = "<img src='" . esc_url($src) . "' alt='$alt'$class />"; 
  50.  
  51. /** 
  52. * Filters the image URL sent to the editor. 
  53. * @since 2.8.0 
  54. * @param string $html HTML markup sent to the editor for an image. 
  55. * @param string $src Image source URL. 
  56. * @param string $alt Image alternate, or alt, text. 
  57. * @param string $align The image alignment. Default 'alignnone'. Possible values include 
  58. * 'alignleft', 'aligncenter', 'alignright', 'alignnone'. 
  59. */ 
  60. $html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align ); 
  61.  
  62. return media_send_to_editor($html); 
  63.  
  64. if ( isset( $_POST['save'] ) ) { 
  65. $errors['upload_notice'] = __('Saved.'); 
  66. wp_enqueue_script( 'admin-gallery' ); 
  67. return wp_iframe( 'media_upload_gallery_form', $errors ); 
  68.  
  69. } elseif ( ! empty( $_POST ) ) { 
  70.  
  71. if ( is_string($return) ) 
  72. return $return; 
  73. if ( is_array($return) ) 
  74. $errors = $return; 
  75.  
  76. if ( isset($_GET['tab']) && $_GET['tab'] == 'type_url' ) { 
  77. $type = 'image'; 
  78. if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ) ) ) 
  79. $type = $_GET['type']; 
  80. return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id ); 
  81.  
  82. return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );