WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

i have attacment.php like this.


if ( $attachments = get_children( array(  
'post_type' => 'attachment',  
'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?

share|improve this question
What do you mean by 'single click to download'? What is happening right now when you click on the link? – tfrommen Mar 15 '13 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 '13 at 0:20
Well, then this is no WP-specific question. Please have a look over here. – tfrommen Mar 15 '13 at 0:27
thanks you.. 4 ur help. – Angel Mar 15 '13 at 0:42
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) – ChelseaStats Nov 23 '15 at 13:27

It is possible by using the plugin:


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

share|improve this answer

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',
    '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.

share|improve this answer
i have tested your code, but the Javascript is not work well. how resolve this. – Angel Mar 15 '13 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 '13 at 20:34

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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