函数原型
media_sideload_image( string $file, int $post_id, string $desc = null, string $return_type = ‘html’ ): string|int|WP_Error
函数描述
Downloads an image from the specified URL, saves it as an attachment, and optionally attaches it to a post.
是否弃用
未弃用
函数参数
-
$file
string
Required - The URL of the image to download.
-
$post_id
int
Optional - The post ID the media is to be associated with.
-
$desc
string
Optional - Description of the image.
Default:
null -
$return_type
string
Optional - Accepts
'html'(image tag html) or'src'(URL), or'id'(attachment ID). Default'html'.Default:
'html'
函数返回值
string|int|WP_Error Populated HTML img tag, attachment ID, or attachment source on success, WP_Error object otherwise.
函数位置
File: wp-admin/includes/media.php.
函数源码
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');源码链接
变更日志
| Version | Description |
|---|---|
| 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. |

