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:455
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
$_FILESupload 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
- Scaling
- Constant
- Instructions
- 29–100
- Plugin surface
- None
- Called by
- 2
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 111.
Nothing here hands control to plugin code.
2 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 - optionoption read or write
get_option()one call below media_handle_sideload() - hookthird-party callbacks
apply_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 · 15 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.
| When | Instructions | Calls it makes |
|---|---|---|
isset($post_data) && isset($file) | 29 | wp_handle_sideload() |
isset($file) | 31–41 | get_post(), current_time(), wp_handle_sideload() |
isset($file) | 34–39 | get_post(), wp_handle_sideload() |
isset($post_data) && !isset($file) && is_wp_error() | 62–71 | wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error() |
!isset($file) && is_wp_error() | 64–83 | get_post(), current_time(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error() |
!isset($file) && is_wp_error() | 67–81 | get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), post_mime_type(), wp_insert_attachment(), is_wp_error() |
isset($post_data) && !isset($file) && !is_wp_error() | 70–79 | 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) && !is_wp_error() | 72–91 | get_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($post_data) && !isset($file) && is_wp_error() | 75–80 | wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), is_wp_error() |
!isset($file) && !is_wp_error() | 75–89 | get_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) && is_wp_error() | 77–92 | get_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) && is_wp_error() | 80–90 | get_post(), wp_handle_sideload(), wp_basename(), wp_read_image_metadata(), sanitize_title(), post_mime_type(), wp_insert_attachment(), is_wp_error() |
3 further outcomes, up to 98 instructions
isset($post_data) && !isset($file) && !is_wp_error() | 83–88 | 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) && !is_wp_error() | 85–100 | get_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) && !is_wp_error() | 88–98 | get_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() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 111 | 29–100 | 11 | |
| 8.5 | 111 | 29–100 | 11 | |
| 8.4 | 111 | 29–100 | 11 | 16 fewer instructions than PHP 8.3 |
| 8.3 | 127 | 31–116 | 11 | |
| 8.2 | 127 | 31–116 | 11 | |
| 8.1 | 127 | 31–116 | 11 | |
| 7.4 | 127 | 31–116 | 11 |
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 · 11
- get_post()Retrieves post data given a post ID or post object.
- current_time()Retrieves the current time based on specified type.
- wp_handle_sideload()Wrapper for _wp_handle_upload().
- wp_basename()i18n-friendly version of basename().
- wp_read_image_metadata()Gets extended image metadata, exif or iptc as available.
- sanitize_title()Sanitizes a string into a slug, which can be used in URLs or HTML attributes.
- wp_insert_attachment()Inserts an attachment.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- wp_generate_attachment_metadata()Generates attachment meta data and create image sub-sizes for images.
- WP_Error::__construct()Initializes the error.
Used by · 2
- WP_Customize_Manager::import_theme_starter_content()Imports theme starter content into the customized state.
- media_sideload_image()Downloads an image from the specified URL, saves it as an attachment, and optionally attaches it to a post.
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 = ''; // 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 ( 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 ( ! 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.
Signature, return type and hooks compared across 5 parsed releases.
$post_id parameter was made optional.from the docblockAbout 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.