WPDev.one
Core Function
View Source on Trac ↗

media_sideload_image()

media_sideload_image( mixed $file, mixed $post_id = 0, mixed $desc = null, mixed $return_type = 'html' )

Downloads an image from the specified URL, saves it as an attachment, and optionally attaches it to a post.

Parameters 2 required

Name / Type Description
$file
string

The URL of the image to download.

Required
$post_id
int

Optional. The post ID the media is to be associated with.

$desc
string

Optional. Description of the image.

string

$return_type Optional. Accepts 'html' (image tag html) or 'src' (URL), or 'id' (attachment ID). Default 'html'.

Required

Return Value

((string)
(string | int | WP_Error) Populated HTML img tag, attachment ID, or attachment source on success, WP_Error object otherwise.

Function Information

Since Version
2.6.0
Source File
wp-admin/includes/media.php
Advertisement