How to get the originally uploaded image URL in WordPress

The wp_get_original_image_url function in WordPress is designed to retrieve the original URL of an image. This function is applicable in scenarios where the original, full-sized image URL is required, as opposed to a thumbnail or other smaller sized variant of the image.

WordPress often creates multiple versions of an uploaded image in various sizes. While these resized versions are suitable for different contexts within a site, there may be instances where the original image’s URL is necessary. The wp_get_original_image_url function provides a way to access this URL directly.

It’s important to note that this function only works with images that are uploaded to the WordPress media library. Images that are linked from external sources will not work with this function. The function returns the URL of the original image as a string if it exists; if not, it returns false.

Parameters Accepted by wp_get_original_image_url Function

The wp_get_original_image_url function in WordPress accepts a single parameter:

  • $attachment_id (integer): This is a mandatory parameter representing the ID of the attachment post.

Return Value of wp_get_original_image_url Function

The wp_get_original_image_url function returns either a string or a boolean value. Specifically, it provides the URL of the attachment image. However, in case of an error or if the attachment is not an image, it returns false.

Examples

How to Retrieve the Original Image URL in WordPress

In this example, we will use the wp_get_original_image_url function to retrieve the URL of the original uploaded image in WordPress. This is useful when you want to display or manipulate the original image instead of a cropped or resized version.

$attachment_id = 123; // Replace with your attachment_id
$image_url = wp_get_original_image_url($attachment_id);

if ($image_url !== false) {
 echo '<p>Original Image URL: ' . $image_url . '</p>';
} else {
 echo '<p>Unable to retrieve original image URL.</p>';
}

In this code snippet, we first set the $attachment_id variable to the ID of the attachment we’re interested in. We then call the wp_get_original_image_url function with this ID as the parameter. The function returns the URL of the original image, or false if it fails to retrieve the URL or if the attachment is not an image. We then check if $image_url is not false, and if so, we echo out the original image URL. If it is false, we echo out a message indicating that we were unable to retrieve the original image URL.

Conclusion

The wp_get_original_image_url function in WordPress is a built-in function that allows developers to retrieve the original URL of an image. This function is predominantly used in instances where the original, unedited image is required, for example, when the need arises to display the image in its original form, bypassing any changes that might have been made to it after it was uploaded. This function thus plays a significant role in WordPress image management, providing an easy method to access the original image URL.

Related WordPress Functions