media_sideload_image()
云策文档标注
概述
media_sideload_image() 函数用于从指定 URL 下载图像,保存为附件,并可关联到文章。它支持多种返回类型,包括 HTML 图像标签、附件 ID 或 URL。
关键要点
- 参数:$file(必需,图像 URL)、$post_id(可选,关联文章 ID)、$desc(可选,图像描述,实际用作标题)、$return_type(可选,返回类型,默认为 'html')。
- 返回:成功时返回 HTML img 标签、附件 ID 或 URL,失败时返回 WP_Error 对象。
- 扩展名检查:默认允许 jpg、jpeg、jpe、png、gif、webp,可通过 image_sideload_extensions 过滤器修改。
- 外部使用:在非 /wp-admin/ 上下文中使用时,需包含 media.php、file.php 和 image.php 文件。
- 注意事项:$desc 参数实际设置附件标题而非描述;URL 需包含有效扩展名,否则可能失败;函数可扩展用于非图像文件。
代码示例
$url = "https://wordpress.org/about/images/logos/wordpress-logo-stacked-rgb.png";
$post_id = 1;
$desc = "The WordPress Logo";
$image = media_sideload_image( $url, $post_id, $desc );注意事项
- $desc 参数实际用作附件标题,而非描述字段。
- URL 必须包含允许的扩展名,否则返回 WP_Error;对于无扩展名 URL,可尝试添加 #.jpg 绕过检查。
- 可通过 image_sideload_extensions 过滤器添加其他扩展名(如 mp3)以支持非图像文件,但需调整 $return_type。
- 从 WordPress 4.8 开始,可直接通过 $return_type='id' 获取附件 ID。
原文内容
Downloads an image from the specified URL, saves it as an attachment, and optionally attaches it to a post.
Parameters
$filestringrequired-
The URL of the image to download.
$post_idintoptional-
The post ID the media is to be associated with.
$descstringoptional-
Description of the image.
Default:
null $return_typestringoptional-
Accepts
'html'(image tag html) or'src'(URL), or'id'(attachment ID). Default'html'.
Source
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' );
}
}
Hooks
- apply_filters( ‘image_sideload_extensions’, string[] $allowed_extensions, string $file )
-
Filters the list of allowed file extensions when sideloading an image from a URL.
Changelog
| Version | Description |
|---|---|
| 5.8.0 | Added 'webp' to the default list of allowed file extensions. |
| 5.4.0 | The original URL of the attachment is stored in the _source_url post meta value. |
| 5.3.0 | The $post_id parameter was made optional. |
| 4.8.0 | Introduced the 'id' option for the $return_type parameter. |
| 4.2.0 | Introduced the $return_type parameter. |
| 2.6.0 | Introduced. |
Skip to note 7 content
tgiokdi
A word of caution, the “description” that’s the third value for this function is actually the attachment’s “TITLE” and not it’s “DESCRIPTION”.
There is no support for adding the actual description with media_sideload_image.
Skip to note 8 content
Codex
Default Usage
$url = "<a href="https://wordpress.org/about/images/logos/wordpress-logo-stacked-rgb.png"" rel="nofollow ugc">https://wordpress.org/about/images/logos/wordpress-logo-stacked-rgb.png"</a>;; $post_id = 1; $desc = "The WordPress Logo"; $image = media_sideload_image( $url, $post_id, $desc );Skip to note 9 content
andreas83b
First i was very happy about this function, since it fits exactly my use case.
After looking a bit deeper, i noticed that the allowed extension check will block my remote resource.
In my case b-4bd4-b383-f9021e0ba3d2.jpg1
since remote images often don’t have a extension at all i don’t see any value in this extension check, while checking mime types are a better approach in my opinion.
Skip to note 10 content
Mark Parnell
Despite its name, this function can be used to load any type of file to the media library as long as you override the accepted extensions first. For example you can import audio files by adding
mp3to the list of extensions:add_filter( 'image_sideload_extensions', function ( $accepted_extensions ) { $accepted_extensions[] = 'mp3'; return $accepted_extensions; } );Note however that you’ll probably want to set the
$return_typeparameter to eitherurlorid, as an image tag pointing to something other than an image won’t be very useful.Skip to note 11 content
Koen Reus
media_sideload_image()checks the file extension as a basic security measure, but in a lot of cases external image URLs coming from APIs for example do not actually have one, while still serving a perfectly valid image.If you trust the source of the images, you can bypass the check by appending
#.jpgto the url like so:$wp_media_id = media_sideload_image( $url . '#.jpg', 0, null, 'id' );Skip to note 12 content
MakeWebBetter
Find ID of attachment from src by using this function
function wpdocs_fetch_attachment_post_id_from_srcs( $image_src ) { global $wpdb; $query = "SELECT ID FROM {$wpdb->posts} WHERE guid=%s"; $id = $wpdb->prepare( $query, $image_src ); return $id; } // By passing fourth parameter 'src' you will get URL $src = media_sideload_image( $url, $item_id, $desc, 'src' ); wpdocs_fetch_attachment_post_id_from_srcs( $src );$id = media_sideload_image( $url, $item_id, $desc, 'id' );