media_upload_form_handler(): null|array|void
- Since
- 2.5.0
- Source
wp-admin/includes/media.php:758
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|array|void- Array of error messages keyed by attachment ID, null or void on success.
Performance profile
How much work a call to media_upload_form_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
- Scales with input
- Instructions
- 19–86
- Plugin surface
- 2 hooks
- Called by
- 3
Reaches the database via get_post().
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 199.
Third-party callbacks on 'attachment_fields_to_save', 'media_send_to_editor' run inside this call, and their cost is not bounded by anything here.
3 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_delete()one call below media_upload_form_handler() - optionoption read or write
get_option()one call below media_upload_form_handler()
Further down the call graph this can also reach 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 · 20 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost media_upload_form_handler() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($value) && !isset($send_id) | 19–24 | check_admin_referer() |
!isset($send_id) | 19–27 | check_admin_referer(), exit() |
!isset($send_id) | 29–34 | check_admin_referer(), array_keys(), reset() |
isset($value) && !isset($send_id) | 29–37 | check_admin_referer(), array_keys(), reset(), exit() |
!isset($value) && isset($send_id) && empty($attachment) | 42–48 | check_admin_referer(), wp_unslash(), apply_filters(), media_send_to_editor() |
isset($send_id) && empty($attachment) | 42–51 | check_admin_referer(), exit(), wp_unslash(), apply_filters(), media_send_to_editor() |
isset($send_id) && empty($attachment) | 52–58 | check_admin_referer(), array_keys(), reset(), wp_unslash(), apply_filters(), media_send_to_editor() |
isset($value) && isset($send_id) && empty($attachment) | 52–61 | check_admin_referer(), array_keys(), reset(), exit(), wp_unslash(), apply_filters(), media_send_to_editor() |
!isset($value) && isset($send_id) && !empty($attachment) | 61–67 | check_admin_referer(), wp_unslash(), esc_attr(), apply_filters(), media_send_to_editor() |
!isset($value) && isset($send_id) && !empty($attachment) | 61–67 | check_admin_referer(), wp_unslash(), get_attachment_link(), apply_filters(), media_send_to_editor() |
isset($send_id) && !empty($attachment) | 61–70 | check_admin_referer(), exit(), wp_unslash(), esc_attr(), apply_filters(), media_send_to_editor() |
isset($send_id) && !empty($attachment) | 61–70 | check_admin_referer(), exit(), wp_unslash(), get_attachment_link(), apply_filters(), media_send_to_editor() |
8 further outcomes, up to 86 instructions
!isset($value) && isset($send_id) && !empty($attachment) | 67–73 | check_admin_referer(), wp_unslash(), get_attachment_link(), esc_attr(), apply_filters(), media_send_to_editor() |
isset($send_id) && !empty($attachment) | 67–76 | check_admin_referer(), exit(), wp_unslash(), get_attachment_link(), esc_attr(), apply_filters(), media_send_to_editor() |
isset($send_id) && !empty($attachment) | 71–77 | check_admin_referer(), array_keys(), reset(), wp_unslash(), esc_attr(), apply_filters(), media_send_to_editor() |
isset($send_id) && !empty($attachment) | 71–77 | check_admin_referer(), array_keys(), reset(), wp_unslash(), get_attachment_link(), apply_filters(), media_send_to_editor() |
isset($value) && isset($send_id) && !empty($attachment) | 71–80 | check_admin_referer(), array_keys(), reset(), exit(), wp_unslash(), esc_attr(), apply_filters(), media_send_to_editor() |
isset($value) && isset($send_id) && !empty($attachment) | 71–80 | check_admin_referer(), array_keys(), reset(), exit(), wp_unslash(), get_attachment_link(), apply_filters(), media_send_to_editor() |
isset($send_id) && !empty($attachment) | 77–83 | check_admin_referer(), array_keys(), reset(), wp_unslash(), get_attachment_link(), esc_attr(), apply_filters(), media_send_to_editor() |
isset($value) && isset($send_id) && !empty($attachment) | 77–86 | check_admin_referer(), array_keys(), reset(), exit(), wp_unslash(), get_attachment_link(), esc_attr(), apply_filters(), media_send_to_editor() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 199 | 19–86 | 26 | |
| 8.5 | 199 | 19–86 | 26 | |
| 8.4 | 199 | 19–86 | 26 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 201 | 15–86 | 26 | |
| 8.2 | 201 | 15–86 | 26 | |
| 8.1 | 201 | 15–86 | 26 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 202 | 15–87 | 26 |
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 media_upload_form_handler() runs, in this order:
- apply_filters( attachment_fields_to_save )filterline 809 (+51 into the body)
Filters the attachment fields to be saved.
- apply_filters( media_send_to_editor )filterline 875 (+117 into the body)
Filters the HTML markup for a media item 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.
- get_post()Retrieves post data given a post ID or post object.
- current_user_can()Returns whether the current user has the specified capability.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_unslash()Removes slashes from a string or recursively removes slashes from strings within an array.
- get_post_meta()Retrieves a post meta field for the given post ID.
- wp_strip_all_tags()Properly strips all HTML tags including 'script' and 'style'.
- update_post_meta()Updates a post meta field based on the given post ID.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- wp_update_post()Updates a post with new post data.
- get_attachment_taxonomies()Retrieves taxonomies attached to given the attachment.
- wp_set_object_terms()Creates term and taxonomy relationships.
Show all 16
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- get_attachment_link()Retrieves the permalink for an attachment.
- esc_attr()Escaping for HTML attributes.
- media_send_to_editor()Adds image HTML to editor.
Used by · 3
- media_upload_gallery()Retrieves the legacy media uploader form in an iframe.
- media_upload_library()Retrieves the legacy media library form in an iframe.
- wp_media_upload_handler()Handles the process of uploading media.
Source code
function media_upload_form_handler() { check_admin_referer( 'media-form' ); $errors = null; if ( isset( $_POST['send'] ) ) { $keys = array_keys( $_POST['send'] ); $send_id = (int) reset( $keys ); } if ( ! empty( $_POST['attachments'] ) ) { foreach ( $_POST['attachments'] as $attachment_id => $attachment ) { $post = get_post( $attachment_id, ARRAY_A ); $_post = $post; if ( ! current_user_can( 'edit_post', $attachment_id ) ) { continue; } if ( isset( $attachment['post_content'] ) ) { $post['post_content'] = $attachment['post_content']; } if ( isset( $attachment['post_title'] ) ) { $post['post_title'] = $attachment['post_title']; } if ( isset( $attachment['post_excerpt'] ) ) { $post['post_excerpt'] = $attachment['post_excerpt']; } if ( isset( $attachment['menu_order'] ) ) { $post['menu_order'] = $attachment['menu_order']; } if ( isset( $send_id ) && $attachment_id === $send_id ) { if ( isset( $attachment['post_parent'] ) ) { $post['post_parent'] = $attachment['post_parent']; } } /** * Filters the attachment fields to be saved. * * @since 2.5.0 * * @see wp_get_attachment_metadata() * * @param array $post An array of post data. * @param array $attachment An array of attachment metadata. */ $post = apply_filters( 'attachment_fields_to_save', $post, $attachment ); if ( isset( $attachment['image_alt'] ) ) { $image_alt = wp_unslash( $attachment['image_alt'] ); if ( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) !== $image_alt ) { $image_alt = wp_strip_all_tags( $image_alt, true ); // update_post_meta() expects slashed. update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); } } if ( isset( $post['errors'] ) ) { $errors[ $attachment_id ] = $post['errors']; unset( $post['errors'] ); } if ( $post != $_post ) { wp_update_post( $post ); } foreach ( get_attachment_taxonomies( $post ) as $t ) { if ( isset( $attachment[ $t ] ) ) { wp_set_object_terms( $attachment_id, array_map( 'trim', preg_split( '/,+/', $attachment[ $t ] ) ), $t, false ); } } } }Changelog
Introduced in 2.5.0. One change between 6.7.7 and 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
null|array|void to null|array.verified against sourceAbout 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.