Function: media_sideload_image

media_sideload_image( string $file, integer $post_id, string $desc, string $return )

Downloads an image from the specified URL and attaches it to a post.

Parameters

Name Type(s) Default Value Description
$file string

The URL of the image to download.

$post_id integer

The post ID the media is to be associated with.

$desc string null

Optional. Description of the image.

$return string 'html'

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

Returns

string | WP_Error

Populated HTML img tag on success, WP_Error object otherwise.

WordPress Developer Newsletter

Stay on top of the latest WordPress API changes, developer tool updates, security alerts and more.