You can trigger a remote image download from Infinite Uploads cloud storage using the new download_image() function. This is especially useful for integrations such as ShortPixel, on-demand image optimization, or any workflow that requires a file to exist locally before processing.
This helper ensures the image is fetched from cloud storage, saved to the correct local uploads path, and returned for further processing.
Function Overview
download_image( string $relative_path ): string|WP_Error
Parameters
| Name | Type | Description |
|---|---|---|
$relative_path | string | The relative path (within the uploads directory) to the file you want to download. Example: 2025/01/my-image.jpg. |
Returns
- String: The full local filesystem path to the downloaded file
WP_Error: If the file could not be downloaded or written locally
Example Use Case: Integrating with ShortPixel
ShortPixel requires local access to the file it is optimizing. When the asset is stored remotely in Infinite Uploads, it may not exist locally. Using download_image() ensures the file is present before triggering optimization.
Basic Example
$relative_path = '2025/01/my-photo.jpg';
// Attempt to download from Infinite Uploads cloud storage.
$local_file = download_image( $relative_path );
if ( is_wp_error( $local_file ) ) {
error_log( 'Failed to download image: ' . $local_file->get_error_message() );
return;
}
// Hand the file off to ShortPixel or any other local processor.
shortpixel_optimize_image( $local_file );
How It Works
When you call download_image():
- Infinite Uploads checks whether the file exists locally.
- If not available, it retrieves the remote object from Infinite Uploads cloud storage.
- The plugin writes the file to the correct location inside
/wp-content/uploads/…. - The function returns the physical local path so you can work with the file normally.
This gives developers a seamless way to interact with cloud-stored media as if it were local.
Common Uses
- Image optimization pipelines (ShortPixel, EWWW, Imagify, etc.)
- Regenerating thumbnails
- Custom batch processors that rely on the local filesystem
- Migrating or syncing media libraries
- Any plugin that needs local access to a remote-stored file
Error Handling
Always check for errors:
$result = download_image( $relative_path );
if ( is_wp_error( $result ) ) {
// Handle gracefully
wp_send_json_error( $result->get_error_message() );
}
Typical error scenarios include:
- File not found in remote storage
- Insufficient write permissions on the server
- Connectivity issues during retrieval
Notes for Developers
$relative_pathmust NOT begin with a slash. Use WordPress’ standard uploads-relative format.- The function is designed to be lightweight; it only downloads the file if not already present.
- Works seamlessly with Infinite Uploads URL rewriting and file syncing.