LLMS_Abstract_Generator_Posts::sideload_image( int $post_id, string $url,  $return = 'src' )

Sideload an image from a url


Description Description


Parameters Parameters

$post_id

(int) (Required) WP_Post ID of the post where the image will be attached.

$url

(string) (Required) The image's URL.


Top ↑

Return Return

(string|int|WP_Error) Returns a WP_Error on failure, the image's new URL when $return is "src", otherwise returns the image's attachment ID.


Top ↑

Source Source

File: includes/abstracts/llms-abstract-generator-posts.php

	protected function sideload_image( $post_id, $url, $return = 'src' ) {

		// Check if the image was previously sideloaded.
		$id = empty( $this->images[ $url ] ) ? false : $this->images[ $url ];

		// Image was not previously sideloaded.
		if ( ! $id ) {

			$id = media_sideload_image( $url, $post_id, null, 'id' );
			if ( is_wp_error( $id ) ) {
				return $id;
			}

			// Store the ID for future usage.
			$this->images[ $url ] = $id;

		}

		return 'src' === $return ? wp_get_attachment_url( $id ) : $id;

	}

Top ↑

Changelog Changelog

Changelog
Version Description
4.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.