media_sideload_image

Advertisement

Summery Summery

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

Syntax Syntax

media_sideload_image( string $file, int $post_id, string $desc = null, string $return = 'html' )

Parameters Parameters

$file

(Required) The URL of the image to download.

$post_id

(Required) The post ID the media is to be associated with.

$desc

(Optional) Description of the image.

Default value: null

$return

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

Default value: 'html'

Return Return

(string|WP_Error) Populated HTML img tag on success, WP_Error object otherwise.

Source Source

File: wp-admin/includes/media.php

			$html = apply_filters( 'image_send_to_editor_url', $html, esc_url_raw( $src ), $alt, $align );
		}

		return media_send_to_editor( $html );
	}

	if ( isset( $_POST['save'] ) ) {
		$errors['upload_notice'] = __( 'Saved.' );
		wp_enqueue_script( 'admin-gallery' );

		return wp_iframe( 'media_upload_gallery_form', $errors );

	} elseif ( ! empty( $_POST ) ) {
		$return = media_upload_form_handler();

		if ( is_string( $return ) ) {
			return $return;
		}

		if ( is_array( $return ) ) {
			$errors = $return;
		}
	}

	if ( isset( $_GET['tab'] ) && 'type_url' === $_GET['tab'] ) {
		$type = 'image';

		if ( isset( $_GET['type'] ) && in_array( $_GET['type'], array( 'video', 'audio', 'file' ), true ) ) {
			$type = $_GET['type'];
		}

		return wp_iframe( 'media_upload_type_url_form', $type, $errors, $id );
	}

	return wp_iframe( 'media_upload_type_form', 'image', $errors, $id );
}

/**
 * Downloads an image from the specified URL and attaches it to a post.
 *
 * @since 2.6.0
 * @since 4.2.0 Introduced the `$return` parameter.
 * @since 4.8.0 Introduced the 'id' option within the `$return` parameter.
 * @since 5.3.0 The `$post_id` parameter was made optional.
 * @since 5.4.0 The original URL of the attachment is stored in the `_source_url`
 *              post meta value.
 *
 * @param string $file    The URL of the image to download.

Advertisement

Changelog Changelog

Changelog
Version Description
4.8.0 Introduced the 'id' option within the $return parameter.
4.2.0 Introduced the $return parameter.
2.6.0 Introduced.

Advertisement

Leave a Reply