wppaste
WordPress

media_upload_form_handler(): null|array|void

Since
2.5.0
Source
wp-admin/includes/media.php:741
Handles form submissions for the legacy media uploader.

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

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
19–87

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

Plugin surface
2 hooks

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.

Called by
3

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

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_delete()one call below media_upload_form_handler()
  • optionoption read or writeget_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.

WhenInstructionsCalls it makes
!isset($value) && !isset($send_id)19–24check_admin_referer()
!isset($send_id)19–27check_admin_referer(), exit()
!isset($send_id)29–34check_admin_referer(), array_keys(), reset()
isset($value) && !isset($send_id)29–37check_admin_referer(), array_keys(), reset(), exit()
!isset($value) && isset($send_id) && empty($attachment)43–49check_admin_referer(), wp_unslash(), apply_filters(), media_send_to_editor()
isset($send_id) && empty($attachment)43–52check_admin_referer(), exit(), wp_unslash(), apply_filters(), media_send_to_editor()
isset($send_id) && empty($attachment)53–59check_admin_referer(), array_keys(), reset(), wp_unslash(), apply_filters(), media_send_to_editor()
isset($value) && isset($send_id) && empty($attachment)53–62check_admin_referer(), array_keys(), reset(), exit(), wp_unslash(), apply_filters(), media_send_to_editor()
!isset($value) && isset($send_id) && !empty($attachment)62–68check_admin_referer(), wp_unslash(), esc_attr(), apply_filters(), media_send_to_editor()
!isset($value) && isset($send_id) && !empty($attachment)62–68check_admin_referer(), wp_unslash(), get_attachment_link(), apply_filters(), media_send_to_editor()
isset($send_id) && !empty($attachment)62–71check_admin_referer(), exit(), wp_unslash(), esc_attr(), apply_filters(), media_send_to_editor()
isset($send_id) && !empty($attachment)62–71check_admin_referer(), exit(), wp_unslash(), get_attachment_link(), apply_filters(), media_send_to_editor()
8 further outcomes, up to 87 instructions
!isset($value) && isset($send_id) && !empty($attachment)68–74check_admin_referer(), wp_unslash(), get_attachment_link(), esc_attr(), apply_filters(), media_send_to_editor()
isset($send_id) && !empty($attachment)68–77check_admin_referer(), exit(), wp_unslash(), get_attachment_link(), esc_attr(), apply_filters(), media_send_to_editor()
isset($send_id) && !empty($attachment)72–78check_admin_referer(), array_keys(), reset(), wp_unslash(), esc_attr(), apply_filters(), media_send_to_editor()
isset($send_id) && !empty($attachment)72–78check_admin_referer(), array_keys(), reset(), wp_unslash(), get_attachment_link(), apply_filters(), media_send_to_editor()
isset($value) && isset($send_id) && !empty($attachment)72–81check_admin_referer(), array_keys(), reset(), exit(), wp_unslash(), esc_attr(), apply_filters(), media_send_to_editor()
isset($value) && isset($send_id) && !empty($attachment)72–81check_admin_referer(), array_keys(), reset(), exit(), wp_unslash(), get_attachment_link(), apply_filters(), media_send_to_editor()
isset($send_id) && !empty($attachment)78–84check_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)78–87check_admin_referer(), array_keys(), reset(), exit(), wp_unslash(), get_attachment_link(), esc_attr(), apply_filters(), media_send_to_editor()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev20119–8726
8.520119–8726
8.420119–87262 fewer instructions than PHP 8.3
8.320315–8726
8.220315–8726
8.120315–87262 fewer instructions than PHP 7.4
7.420515–8926

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:

  1. apply_filters( attachment_fields_to_save )filterline 792 (+51 into the body)

    Filters the attachment fields to be saved.

  2. apply_filters( media_send_to_editor )filterline 858 (+117 into the body)

    Filters the HTML markup for a media item sent to the editor.

Uses · 16

Show all 16

Used by · 3

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.

  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.

7.1.0
Return type changed from null|array|void to null|array.verified against source
2.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.