wppaste
WordPress

media_handle_sideload( string[] $file_array, int $post_id = 0, string $desc = null, array $post_data = array() ): int|WP_Error

Since
2.6.0, 5.3.0
Source
wp-admin/includes/media.php:465
Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().

Compatibility

WordPress
since 5.3.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.

Parameters

$file_arraystring[]
Array that represents a $_FILES upload array.
$post_idintoptional
The post ID the media is associated with.Default: 0
$descstringoptional
Description of the side-loaded file. Default null.Default: null
$post_dataarrayoptional
Post data to override. Default empty array.Default: array()

Return value

int|WP_Error
The ID of the attachment or a WP_Error on failure.

Performance profile

How much work a call to media_handle_sideload() 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
29–116

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

Plugin surface
None

Nothing here hands control to plugin code.

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
  • optionoption read or writeget_option()one call below media_handle_sideload()
  • hookthird-party callbacksapply_filters()one call below media_handle_sideload()

Further down the call graph this can also reach 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 · 27 distinct outcomes

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

WhenInstructionsCalls it makes
isset($post_data) && isset($file)29wp_handle_sideload()
isset($file)31–41get_post(), current_time(), wp_handle_sideload()
isset($file)34–39get_post(), wp_handle_sideload()
isset($post_data) && !isset($file) && !$alt && is_wp_error()65–79wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error()
!isset($file) && !$alt && is_wp_error()67–91get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error()
!isset($file) && !$alt && is_wp_error()70–89get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error()
isset($post_data) && !isset($file) && !$alt && !is_wp_error()73–87wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
isset($post_data) && !isset($file) && $alt && is_wp_error()73–87wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error()
!isset($file) && !$alt && !is_wp_error()75–99get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
!isset($file) && $alt && is_wp_error()75–99get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error()
!isset($file) && !$alt && !is_wp_error()78–97get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
!isset($file) && $alt && is_wp_error()78–97get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error()
15 further outcomes, up to 114 instructions
isset($post_data) && !isset($file) && !$alt && is_wp_error()81–88wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), is_wp_error()
isset($post_data) && !isset($file) && $alt && !is_wp_error()81–95wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
!isset($file) && !$alt && is_wp_error()83–100get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), is_wp_error()
!isset($file) && $alt && !is_wp_error()83–107get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
!isset($file) && !$alt && is_wp_error()86–98get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), is_wp_error()
!isset($file) && $alt && !is_wp_error()86–105get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
isset($post_data) && !isset($file) && !$alt && !is_wp_error()89–96wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
isset($post_data) && !isset($file) && $alt && is_wp_error()89–96wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error()
!isset($file) && !$alt && !is_wp_error()91–108get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
!isset($file) && $alt && is_wp_error()91–108get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error()
!isset($file) && !$alt && !is_wp_error()94–106get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
!isset($file) && $alt && is_wp_error()94–106get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error()
isset($post_data) && !isset($file) && $alt && !is_wp_error()97–104wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
!isset($file) && $alt && !is_wp_error()99–116get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()
!isset($file) && $alt && !is_wp_error()102–114get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), sanitize_text_field(), update_post_meta(), is_wp_error(), wp_generate_attachment_metadata(), wp_update_attachment_metadata()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev12729–11613
8.512729–11613
8.412729–1161320 fewer instructions than PHP 8.3
8.314731–13613
8.214731–13613
8.114731–13613
7.414731–13613

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.

Uses · 13

Show all 13

Used by · 3

Source code

function media_handle_sideload( $file_array, $post_id = 0, $desc = null, $post_data = array() ) {	$overrides = array( 'test_form' => false ); 	if ( isset( $post_data['post_date'] ) && substr( $post_data['post_date'], 0, 4 ) > 0 ) {		$time = $post_data['post_date'];	} else {		$post = get_post( $post_id );		if ( $post && substr( $post->post_date, 0, 4 ) > 0 ) {			$time = $post->post_date;		} else {			$time = current_time( 'mysql' );		}	} 	$file = wp_handle_sideload( $file_array, $overrides, $time ); 	if ( isset( $file['error'] ) ) {		return new WP_Error( 'upload_error', $file['error'] );	} 	$url     = $file['url'];	$type    = $file['type'];	$file    = $file['file'];	$title   = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );	$content = '';	$alt     = ''; 	// Use image exif/iptc data for title and caption defaults if possible.	$image_meta = wp_read_image_metadata( $file ); 	if ( $image_meta ) {		if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {			$title = $image_meta['title'];		} 		if ( trim( $image_meta['caption'] ) ) {			$content = $image_meta['caption'];		}		if ( trim( $image_meta['alt'] ) ) {			$alt = $image_meta['alt'];		}	} 	if ( isset( $desc ) ) {		$title = $desc;	} 	// Construct the attachment array.	$attachment = array_merge(		array(			'post_mime_type' => $type,			'guid'           => $url,			'post_parent'    => $post_id,			'post_title'     => $title,			'post_content'   => $content,		),		$post_data	); 	// This should never be set as it would then overwrite an existing attachment.	unset( $attachment['ID'] ); 	// Save the attachment metadata.	$attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true ); 	if ( trim( $alt ) ) {		update_post_meta( $attachment_id, '_wp_attachment_image_alt', sanitize_text_field( $alt ) );	} 	if ( ! is_wp_error( $attachment_id ) ) {		wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );	} 	return $attachment_id;}

Changelog

Introduced in 2.6.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.

5.3.0
The $post_id parameter was made optional.from the docblock
2.6.0
Introduced.from the docblock

About this page

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