wppaste
WordPress

wp_media_upload_handler(): null|string

Since
2.5.0
Source
wp-admin/includes/media.php:890
Handles the process of uploading media.

Compatibility

WordPress
since 2.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

null|string
The form handler result or null.

Performance profile

How much work a call to wp_media_upload_handler() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
19–102

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 211.

Plugin surface
2 hooks

Third-party callbacks on '{$type}_send_to_editor_url', 'image_send_to_editor_url' run inside this call, and their cost is not bounded by anything here.

Called by
4

4 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • querycontent queryget_post()one call below wp_media_upload_handler()

Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 32 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_media_upload_handler() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!isset($value) && is_string($return)19–21media_upload_form_handler()
!isset($value) && empty($value)23–40wp_iframe()
empty($value) && isset($value)24–26__(), wp_enqueue_script(), wp_iframe()
!isset($value) && !is_string($return)30–48media_upload_form_handler(), wp_iframe()
is_string($return)37–40check_admin_referer(), media_handle_upload(), is_wp_error(), media_upload_form_handler()
always41–59check_admin_referer(), media_handle_upload(), is_wp_error(), wp_iframe()
isset($value)42–45check_admin_referer(), media_handle_upload(), is_wp_error(), __(), wp_enqueue_script(), wp_iframe()
!isset($value) && !empty($value) && empty($src)46–56wp_unslash(), esc_attr(), sanitize_url(), apply_filters(), media_send_to_editor()
!is_string($return)48–67check_admin_referer(), media_handle_upload(), is_wp_error(), media_upload_form_handler(), wp_iframe()
!empty($value) && isset($value) && !empty($title) && !$152–59wp_unslash(), esc_html(), sanitize_url(), apply_filters(), media_send_to_editor()
!isset($value) && !empty($value) && !empty($src)56–66wp_unslash(), esc_attr(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && isset($value) && !empty($title) && $159–68wp_unslash(), esc_html(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor()
20 further outcomes, up to 102 instructions
!empty($value) && empty($src) && isset($value)59–69wp_unslash(), esc_attr(), wp_unslash(), esc_attr(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && isset($value) && empty($title) && !$159–66wp_unslash(), esc_html(), wp_basename(), esc_html(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && isset($value) && !empty($title) && !$161–67wp_unslash(), esc_html(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && empty($src)64–75check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_attr(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && isset($value) && empty($title) && $166–75wp_unslash(), esc_html(), wp_basename(), esc_html(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && isset($value) && !empty($title) && $168–76wp_unslash(), esc_html(), esc_url(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && isset($value) && empty($title) && !$168–74wp_unslash(), esc_html(), wp_basename(), esc_html(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && isset($value) && !empty($src)69–79wp_unslash(), esc_attr(), wp_unslash(), esc_attr(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && !empty($title) && !$170–78check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_html(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && !empty($src)74–85check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_attr(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor()
!empty($value) && isset($value) && empty($title) && $175–83wp_unslash(), esc_html(), wp_basename(), esc_html(), esc_url(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && !empty($title) && $177–87check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_html(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && empty($src)77–88check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_attr(), wp_unslash(), esc_attr(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && empty($title) && !$177–85check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_html(), wp_basename(), esc_html(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && !empty($title) && !$179–86check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_html(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && empty($title) && $184–94check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_html(), wp_basename(), esc_html(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && !empty($title) && $186–95check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_html(), esc_url(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && empty($title) && !$186–93check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_html(), wp_basename(), esc_html(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && !empty($src)87–98check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_attr(), wp_unslash(), esc_attr(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor()
isset($value) && !empty($value) && empty($title) && $193–102check_admin_referer(), media_handle_upload(), is_wp_error(), wp_unslash(), esc_html(), wp_basename(), esc_html(), esc_url(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev21119–10224
8.521119–10224
8.421119–102247 fewer instructions than PHP 8.3
8.321819–10924
8.221819–10924
8.121819–10924
7.421819–10924

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_media_upload_handler() runs, in this order:

  1. apply_filters( {$type}_send_to_editor_url )filterline 951 (+61 into the body)

    Filters the URL sent to the editor for a specific media type.

  2. apply_filters( image_send_to_editor_url )filterline 976 (+86 into the body)

    Filters the image URL sent to the editor.

Uses · 16

Show all 16

Used by · 4

Source code

function wp_media_upload_handler() {	$errors = array();	$id     = 0; 	if ( isset( $_POST['html-upload'] ) && ! empty( $_FILES ) ) {		check_admin_referer( 'media-form' );		// Upload File button was clicked.		$id = media_handle_upload( 'async-upload', $_REQUEST['post_id'] );		unset( $_FILES ); 		if ( is_wp_error( $id ) ) {			$errors['upload_error'] = $id;			$id                     = false;		}	} 	if ( ! empty( $_POST['insertonlybutton'] ) ) {		$src = $_POST['src']; 		if ( ! empty( $src ) && ! strpos( $src, '://' ) ) {			$src = "http://$src";		} 		if ( isset( $_POST['media_type'] ) && 'image' !== $_POST['media_type'] ) {			$title = esc_html( wp_unslash( $_POST['title'] ) );			if ( empty( $title ) ) {				$title = esc_html( wp_basename( $src ) );			} 			if ( $title && $src ) {				$html = "<a href='" . esc_url( $src ) . "'>$title</a>";			} 			$type = 'file';			$ext  = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ); 			if ( $ext ) {				$ext_type = wp_ext2type( $ext );				if ( 'audio' === $ext_type || 'video' === $ext_type ) {					$type = $ext_type;				}			} 			/**			 * Filters the URL sent to the editor for a specific media type.			 *			 * The dynamic portion of the hook name, `$type`, refers to the type			 * of media being sent.			 *			 * Possible hook names include:			 *			 *  - `audio_send_to_editor_url`			 *  - `file_send_to_editor_url`			 *  - `video_send_to_editor_url`			 *			 * @since 3.3.0			 *			 * @param string $html  HTML markup sent to the editor.			 * @param string $src   Media source URL.			 * @param string $title Media title.			 */			$html = apply_filters( "{$type}_send_to_editor_url", $html, sanitize_url( $src ), $title );		} else {			$align = '';			$alt   = esc_attr( wp_unslash( $_POST['alt'] ) ); 			if ( isset( $_POST['align'] ) ) {				$align = esc_attr( wp_unslash( $_POST['align'] ) );				$class = " class='align$align'";			} 			if ( ! empty( $src ) ) {				$html = "<img src='" . esc_url( $src ) . "' alt='$alt'$class />";			} 			/**			 * Filters the image URL sent to the editor.			 *			 * @since 2.8.0			 *

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-admin/includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.