The download_url()
function in WordPress enables developers to download a file from the specified URL into the WordPress filesystem. It can be used to download external files, such as images, audio or video files, or other documents, and store them in the WordPress uploads directory. The function returns the full path to the downloaded file on success, or a WP_Error object on failure. It is important to note that this function does not handle the download or saving of the downloaded file, just the initial download.
What the function returns
-
string|WP_Error
-
Filename on success, WP_Error on failure
How to use the function
download_url( string $url, int $timeout = 300, bool $signature_verification = false )
Parameters
-
$url
string -
The URL of the
file
to download -
$timeout
int -
The timeout for the request to download the file
Default:
300
-
$signature_verification
bool -
Whether to perform Signature Verification
Default:
false
Examples of using the function
#1 Basic example of using the function
// Get the URL of the file that needs to be downloaded
$url = 'http://example.com/files/file.zip';
// Download the file using download_url()
$temp_file = download_url( $url );
// Check for errors
if( is_wp_error( $temp_file ) ) {
// Error occurred
} else {
// No Errors
}