Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
What do you mean by 'single click to download'? What is happening right now when you click on the link? –  t f 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. –  t f Mar 15 '13 at 0:27
    
thanks you.. 4 ur help. –  Angel Mar 15 '13 at 0:42
add comment

2 Answers 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)

share|improve this answer
add comment

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.

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
add comment

Your Answer

 
discard

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.