Make Target _Blank With This Attacment PHP

this php below will print like this http://example.com/wp-content/uploads/2013/01/imagename.jpg with anchor text ‘DOWNLOAD’

<?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');  
}  
?> 

1.when user clicking this link, how to target in _blank or open in new tab.

Read More

2.is possible this short code combine with Javascript to make Force download link? look like bellow.

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>';
}

Related posts

Leave a Reply

2 comments

  1. This is what I meant below in my response.

    array(  
    'post_type' => 'attachment',  
    'post_mime_type'=>'image',  
    'numberposts' => 1,  
    'post_status' => null,  
    'post_parent' => $post->ID ,
    'target' => 'target="_blank"'; 
    )
    

    See if it works this way.

  2. amek teh chages and try this

     foreach ( $attachments as $attachment ) {
    echo '<a href="javascript:void(0);"
        onclick="document.execCommand('SaveAs', true, '' . get_permalink( $attachment->ID ) . '');" target="blank">
        Download This Wallpaper</a>';
    

    }

    hope this will helpt you