wp_media_upload_handler(): null|string
- Since
- 2.5.0
- Source
wp-admin/includes/media.php:873
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
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
- Scaling
- Constant
- Instructions
- 19–102
- Plugin surface
- 2 hooks
- Called by
- 4
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 211.
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.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($value) && is_string($return) | 19–21 | media_upload_form_handler() |
!isset($value) && empty($value) | 23–40 | wp_iframe() |
empty($value) && isset($value) | 24–26 | __(), wp_enqueue_script(), wp_iframe() |
!isset($value) && !is_string($return) | 30–48 | media_upload_form_handler(), wp_iframe() |
is_string($return) | 37–40 | check_admin_referer(), media_handle_upload(), is_wp_error(), media_upload_form_handler() |
| always | 41–59 | check_admin_referer(), media_handle_upload(), is_wp_error(), wp_iframe() |
isset($value) | 42–45 | check_admin_referer(), media_handle_upload(), is_wp_error(), __(), wp_enqueue_script(), wp_iframe() |
!isset($value) && !empty($value) && empty($src) | 46–56 | wp_unslash(), esc_attr(), sanitize_url(), apply_filters(), media_send_to_editor() |
!is_string($return) | 48–67 | check_admin_referer(), media_handle_upload(), is_wp_error(), media_upload_form_handler(), wp_iframe() |
!empty($value) && isset($value) && !empty($title) && !$1 | 52–59 | wp_unslash(), esc_html(), sanitize_url(), apply_filters(), media_send_to_editor() |
!isset($value) && !empty($value) && !empty($src) | 56–66 | wp_unslash(), esc_attr(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor() |
!empty($value) && isset($value) && !empty($title) && $1 | 59–68 | wp_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–69 | wp_unslash(), esc_attr(), wp_unslash(), esc_attr(), sanitize_url(), apply_filters(), media_send_to_editor() |
!empty($value) && isset($value) && empty($title) && !$1 | 59–66 | wp_unslash(), esc_html(), wp_basename(), esc_html(), sanitize_url(), apply_filters(), media_send_to_editor() |
!empty($value) && isset($value) && !empty($title) && !$1 | 61–67 | wp_unslash(), esc_html(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor() |
!empty($value) && empty($src) | 64–75 | check_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) && $1 | 66–75 | wp_unslash(), esc_html(), wp_basename(), esc_html(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor() |
!empty($value) && isset($value) && !empty($title) && $1 | 68–76 | wp_unslash(), esc_html(), esc_url(), wp_ext2type(), sanitize_url(), apply_filters(), media_send_to_editor() |
!empty($value) && isset($value) && empty($title) && !$1 | 68–74 | wp_unslash(), esc_html(), wp_basename(), esc_html(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor() |
!empty($value) && isset($value) && !empty($src) | 69–79 | wp_unslash(), esc_attr(), wp_unslash(), esc_attr(), esc_url(), sanitize_url(), apply_filters(), media_send_to_editor() |
isset($value) && !empty($value) && !empty($title) && !$1 | 70–78 | check_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–85 | check_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) && $1 | 75–83 | wp_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) && $1 | 77–87 | check_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–88 | check_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) && !$1 | 77–85 | check_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) && !$1 | 79–86 | check_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) && $1 | 84–94 | check_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) && $1 | 86–95 | check_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) && !$1 | 86–93 | check_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–98 | check_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) && $1 | 93–102 | check_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 211 | 19–102 | 24 | |
| 8.5 | 211 | 19–102 | 24 | |
| 8.4 | 211 | 19–102 | 24 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 218 | 19–109 | 24 | |
| 8.2 | 218 | 19–109 | 24 | |
| 8.1 | 218 | 19–109 | 24 | |
| 7.4 | 218 | 19–109 | 24 |
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:
- apply_filters( {$type}_send_to_editor_url )filterline 934 (+61 into the body)
Filters the URL sent to the editor for a specific media type.
- apply_filters( image_send_to_editor_url )filterline 959 (+86 into the body)
Filters the image URL sent to the editor.
Uses · 16
- check_admin_referer()Ensures intent by verifying that a user was referred from another admin page with the correct security nonce.
- media_handle_upload()Saves a file submitted from a POST request and create an attachment post for it.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- esc_html()Escaping for HTML blocks.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- wp_basename()i18n-friendly version of basename().
- esc_url()Checks and cleans a URL.
- wp_ext2type()Retrieves the file type based on the extension name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- sanitize_url()Sanitizes a URL for database or redirect usage.
- esc_attr()Escaping for HTML attributes.
- media_send_to_editor()Adds image HTML to editor.
Show all 16
- __()Retrieves the translation of $text.
- wp_enqueue_script()Enqueues a script.
- wp_iframe()Outputs the iframe to display the media upload page.
- media_upload_form_handler()Handles form submissions for the legacy media uploader.
Used by · 4
- media_upload_audio()Handles uploading an audio file.
- media_upload_file()Handles uploading a generic file.
- media_upload_image()Handles uploading an image.
- media_upload_video()Handles uploading a video file.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.