4

i have attacment.php like this.

<?php  

if ( $attachments = get_children( array(  
'post_type' => 'attachment',  
'post_mime_type'=>'image',  
'numberposts' => 1,  
'post_status' => null,  
'post_parent' => $post->ID  
)));
foreach ($attachments as $attachment) {  
echo wp_get_attachment_link( $attachment->ID, '' , false, true, 'Download This Wallpaper');  
}  
?> 

this code will print attachment link.

My question is: How to make this link to be single click to download image and save to computer user?

4
  • What do you mean by 'single click to download'? What is happening right now when you click on the link?
    – tfrommen
    Mar 15, 2013 at 0:01
  • if I click it now it appears to the page where I upload an image, not downloaded. example example.com/wp-content/uploads/2013/03/filename.jpg link. my question is there a script or combined with the addition of javascript to create this image directly downloaded by the user, without having to right click - SAVE IMAGE AS.
    – Angel
    Mar 15, 2013 at 0:20
  • Well, then this is no WP-specific question. Please have a look over here.
    – tfrommen
    Mar 15, 2013 at 0:27
  • if you could identify your file type of attachment, you could set the page headers of attachment.php to the right mime type and that should force the browser to download (maybe)
    – Mat
    Nov 23, 2015 at 13:27

3 Answers 3

2

It is possible by using the plugin:

http://wordpress.org/extend/plugins/download-shortcode/

I am here to assist you since I am using the same functionnality in my website (force dowloading of post attachements)

1

Although it is not WP-specific, here's how to force the user to download an image:

if ( $attachments = get_posts( array(
    'post_type' => 'attachment',
    'post_mime_type'=>'image',
    'numberposts' => -1,
    'post_status' => 'any',
    'post_parent' => $post->ID,
) ) );
foreach ( $attachments as $attachment ) {
    echo '<a href="javascript:void(0);"
        onclick="document.execCommand(\'SaveAs\', true, \'' . get_permalink( $attachment->ID ) . '\');">
        Download This Wallpaper</a>';
}

Note: the code is untested.

2
  • i have tested your code, but the Javascript is not work well. how resolve this.
    – Angel
    Mar 15, 2013 at 2:12
  • hi there, with my code above, i want to ask how to open the link in _blank format or open in new tab.
    – Angel
    Mar 15, 2013 at 20:34
0

Save the following as image.php on your theme:

<?php

// This forces all image attachments to be downloaded instead of displayed on the browser.
// For it to work, this file needs to be called "image.php".
// For more info, refer to the wp hierarchy diagram.

// Get the path on disk. See https://wordpress.stackexchange.com/a/20087/22510
global $wp_query;
$file = get_attached_file($wp_query->post->ID);

// Force the browser to download. Source: https://wpquestions.com/7521
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public'); //for i.e.
header('Pragma: public');

// ob_clean(); // Looks like we don't need this
flush();
readfile($file);
exit;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.