How to Download Images from the WordPress Media Library

Home » Snippets » How to Download Images from the WordPress Media Library
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Downloading images from the WordPress media library can be a bit confusing, especially if you’re new to the platform. Luckily, the process is actually quite simple and can be done in just a few easy steps. In this article, we’ll show you how to download an image from the WordPress media library so you can use it however you need, whether it’s for a blog post, website design, or other project.

					Sorry, as an AI language model I'm not able to download images or files. But, I can provide you with the code snippet you requested:

function wpturbo_download_media() {
    $attachment_id = 123; // replace with the ID of the attachment you want to download
    $attachment_url = wp_get_attachment_url( $attachment_id );
    header("Content-type:image/jpeg"); // replace with the mime type of your attachment 
    header('Content-disposition:attachment;filename='.basename($attachment_url));
    readfile($attachment_url);
}

add_action( 'wp_ajax_wpturbo_download_media', 'wpturbo_download_media' );
add_action( 'wp_ajax_nopriv_wpturbo_download_media', 'wpturbo_download_media' );
				

The wpturbo_download_media() function is responsible for downloading an image from the WordPress media library. This function is hooked into the wp_ajax_wpturbo_download_media and wp_ajax_nopriv_wpturbo_download_media actions, which allow users to initiate the download process without reloading the entire page.

The first step in the function is to set the ID of the attachment that needs to be downloaded. This ID can be found by going to your WordPress media library and clicking on the image you want to download. The ID can be found in the URL of the attachment page.

$attachment_id = 123; // replace with the ID of the attachment you want to download

Next, we use the wp_get_attachment_url() function to get the URL of the attachment. This URL will be used later to set the file name of the downloaded image.

$attachment_url = wp_get_attachment_url( $attachment_id );

After that, we set the content type of the attachment using the header() function.

header("Content-type:image/jpeg"); // replace with the mime type of your attachment 

The content type should match the file type of the attachment. For example, if the attachment is a PNG file, replace image/jpeg with image/png.

Then, we set the Content-disposition header to tell the browser that this is an attachment and to give it a specific file name. We use the basename() function to get the file name from the attachment_url.

header('Content-disposition:attachment;filename='.basename($attachment_url));

Lastly, we use the readfile() function to output the contents of the attachment to the browser. This is what initiates the download.

readfile($attachment_url);

With this code snippet, you should now be able to download an image from the WordPress media library with ease.

Register an account to save your snippets or go Pro to get more features.