wp_get_original_image_url()WP 5.3.0

Gets the URL of the original image (not its resized copy) by the specified attachment ID.

This function behaves the same as wp_get_attachment_url(), except in the case when a large image has been uploaded and a moderate-sized copy has been created for it. In this case, the function returns the URL of the original image, not the copy, as wp_get_attachment_url(). Read more about this in this note.

Use wp_get_original_image_path() when you need to get the path, not the URL of the original image.

Hooks from the function

Returns

String|false. The URL of the attachment image, false if the attachment is not an image or if the specified attachment does not exist.

Usage

wp_get_original_image_url( $attachment_id );
$attachment_id(integer) (required)
ID of the attachment image.

Examples

0

#1 Getting the original size

Let's say we uploaded a big picture of 8000x6000px to the site. And now when we try to get a big picture we get a URL to a big picture (2056x1650px) but not the original picture, but we want the original one. To do that we use this function.

$attach_id = 200;

wp_get_attachment_url( $attach_id );
// http://example.com/wp-content/uploads/2020/03/jpg-big-image-scaled.jpeg

wp_get_original_image_url( $attach_id ); 
// http://example.com/wp-content/uploads/2020/03/jpg-big-image.jpeg

Changelog

Since 5.3.0 Introduced.

wp_get_original_image_url() code WP 7.0

function wp_get_original_image_url( $attachment_id ) {
	if ( ! wp_attachment_is_image( $attachment_id ) ) {
		return false;
	}

	$image_url = wp_get_attachment_url( $attachment_id );

	if ( ! $image_url ) {
		return false;
	}

	$image_meta = wp_get_attachment_metadata( $attachment_id );

	if ( empty( $image_meta['original_image'] ) ) {
		$original_image_url = $image_url;
	} else {
		$original_image_url = path_join( dirname( $image_url ), $image_meta['original_image'] );
	}

	/**
	 * Filters the URL to the original attachment image.
	 *
	 * @since 5.3.0
	 *
	 * @param string $original_image_url URL to original image.
	 * @param int    $attachment_id      Attachment ID.
	 */
	return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id );
}