media_sideload_image( string $file, int $post_id = 0, string $desc = null, string $return_type = 'html' ): string|int|WP_Error
- Since
- 2.6.0, 4.2.0, 4.8.0, 5.3.0, 5.4.0, 5.8.0
- Source
wp-admin/includes/media.php:1015
Compatibility
- WordPress
- since 5.8.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
$filestring- The URL of the image to download.
$post_idintoptional- The post ID the media is to be associated with.Default:
0 $descstringoptional- Description of the image.Default:
null $return_typestringoptional- Accepts 'html' (image tag html) or 'src' (URL), or 'id' (attachment ID). Default 'html'.Default:
'html'
Return value
string|int|WP_Error- Populated HTML img tag, attachment ID, or attachment source on success, WP_Error object otherwise.
Performance profile
How much work a call to media_sideload_image() 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
- Expensive
- Scaling
- Constant
- Instructions
- 11–85
- Plugin surface
- 1 hook
- Called by
- 0
Reaches an outbound HTTP request via wp_safe_remote_get().
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 109.
Third-party callbacks on 'image_sideload_extensions' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
apply_filters()called directly - filesystemfilesystem access
unlink()called directly - httpoutbound HTTP request
wp_safe_remote_get()one call below media_sideload_image() - querycontent query
get_post()one call below media_sideload_image()
Further down the call graph this can also reach option, serialize, cache 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 · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost media_sideload_image() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($file) | 11–20 | none |
empty($file) && !empty($src) && $return_type !== "src" && isset($desc) | 24 | esc_attr() |
!empty($file) | 35 | apply_filters(), array_map(), preg_match(), __() |
!empty($file) && is_wp_error() | 48 | apply_filters(), array_map(), preg_match(), wp_basename(), download_url(), is_wp_error() |
!empty($file) | 63 | apply_filters(), array_map(), preg_match(), wp_basename(), download_url(), is_wp_error(), media_handle_sideload(), is_wp_error(), unlink() |
!empty($file) && !is_wp_error() && $return_type === "id" | 64 | apply_filters(), array_map(), preg_match(), wp_basename(), download_url(), is_wp_error(), media_handle_sideload(), is_wp_error(), add_post_meta() |
!empty($file) && !is_wp_error() && $return_type !== "id" | 72–81 | apply_filters(), array_map(), preg_match(), wp_basename(), download_url(), is_wp_error(), media_handle_sideload(), is_wp_error(), add_post_meta(), wp_get_attachment_url() |
!empty($file) && !is_wp_error() && $return_type !== "id" && !empty($src) && $return_type !== "src" && isset($desc) | 85 | apply_filters(), array_map(), preg_match(), wp_basename(), download_url(), is_wp_error(), media_handle_sideload(), is_wp_error(), add_post_meta(), wp_get_attachment_url(), esc_attr() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 108 | 11–84 | 8 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 109 | 11–85 | 8 | |
| 8.4 | 109 | 11–85 | 8 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 112 | 11–88 | 8 | |
| 8.2 | 112 | 11–88 | 8 | |
| 8.1 | 112 | 11–88 | 8 | |
| 7.4 | 112 | 11–88 | 8 |
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 · 1
One hook fires while media_sideload_image() runs, in this order:
- apply_filters( image_sideload_extensions )filterline 1038 (+23 into the body)
Filters the list of allowed file extensions when sideloading an image from a URL.
Uses · 10
- apply_filters()Calls the callback functions that have been added to a filter hook.
- __()Retrieves the translation of $text.
- wp_basename()i18n-friendly version of basename().
- download_url()Downloads a URL to a local temporary file using the WordPress HTTP API.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- media_handle_sideload()Handles a side-loaded file in the same way as an uploaded file is handled by media_handle_upload().
- add_post_meta()Adds a meta field to the given post.
- wp_get_attachment_url()Retrieves the URL for an attachment.
- esc_attr()Escaping for HTML attributes.
- WP_Error::__construct()Initializes the error.
Source code
function media_sideload_image( $file, $post_id = 0, $desc = null, $return_type = 'html' ) { if ( ! empty( $file ) ) { $allowed_extensions = array( 'jpg', 'jpeg', 'jpe', 'png', 'gif', 'webp' ); /** * Filters the list of allowed file extensions when sideloading an image from a URL. * * The default allowed extensions are: * * - `jpg` * - `jpeg` * - `jpe` * - `png` * - `gif` * - `webp` * * @since 5.6.0 * @since 5.8.0 Added 'webp' to the default list of allowed file extensions. * * @param string[] $allowed_extensions Array of allowed file extensions. * @param string $file The URL of the image to download. */ $allowed_extensions = apply_filters( 'image_sideload_extensions', $allowed_extensions, $file ); $allowed_extensions = array_map( 'preg_quote', $allowed_extensions ); // Set variables for storage, fix file filename for query strings. preg_match( '/[^\?]+\.(' . implode( '|', $allowed_extensions ) . ')\b/i', $file, $matches ); if ( ! $matches ) { return new WP_Error( 'image_sideload_failed', __( 'Invalid image URL.' ) ); } $file_array = array(); $file_array['name'] = wp_basename( $matches[0] ); // Download file to temp location. $file_array['tmp_name'] = download_url( $file ); // If error storing temporarily, return the error. if ( is_wp_error( $file_array['tmp_name'] ) ) { return $file_array['tmp_name']; } // Do the validation and storage stuff. $id = media_handle_sideload( $file_array, $post_id, $desc ); // If error storing permanently, unlink. if ( is_wp_error( $id ) ) { @unlink( $file_array['tmp_name'] ); return $id; } // Store the original attachment source in meta. add_post_meta( $id, '_source_url', $file ); // If attachment ID was requested, return it. if ( 'id' === $return_type ) { return $id; } $src = wp_get_attachment_url( $id ); } // Finally, check to make sure the file has been saved, then return the HTML. if ( ! empty( $src ) ) { if ( 'src' === $return_type ) { return $src; } $alt = isset( $desc ) ? esc_attr( $desc ) : ''; $html = "<img src='$src' alt='$alt' />"; return $html; } else { return new WP_Error( 'image_sideload_failed' ); }}Changelog
Introduced in 4.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
_source_url post meta value.from the docblock$post_id parameter was made optional.from the docblock$return_type parameter.from the docblock$return_type parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.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.