1

What's the best way of doing this:

On The 'Media Library' page, I would like a link next to 'View' (the 'View' that appears when you hover over a Media item) for 'Download'. The 'Download' hyperlink would link directly to the file, unlike the 'View' link which links to a template-based page with either the embedded image or a link to the non-image file.

I have a lot of PDF's in my media library that I look up regularly, having to click on 'View' and then click on the hyper-linked file name just to get to the file is a little cumbersome.

2 Answers 2

1

Modified version of a piece of code found in this tutorial.

add_filter('media_row_actions', 'wpse_30159_qe_download_link', 10, 2);

function wpse_30159_qe_download_link($actions, $post) {
    /* Almost sure this is not necessary. Just in case... */
    global $current_screen;
    if ( 'upload' != $current_screen->id ) 
        return $actions; 

    // if not PDF file, return default $actions
    if ( 'application/pdf' != $post->post_mime_type )
        return $actions;

    // relative path/name of the file
    $the_file = str_replace(WP_CONTENT_URL, '.', $post->guid);

    // adding the Action to the Quick Edit row
    $actions['Download'] = '<a href="'.WP_CONTENT_URL.'/download.php?file='.$the_file.'">Download</a>';

    return $actions;    
}

The download script resides here: /wp-content/download.php.
And here's a sample code of a force download script.

1

With HTML5 being used it is easier:

//medianattachemeny https://trepmal.com/filter_hook/media_row_actions/
add_filter ('media_row_actions','add_direct_link', 10, 3 );
function add_direct_link( $actions, $post, $detached ) {
    $actions['file_url'] = '<a href="' . wp_get_attachment_url($post->ID) . '" download="' . wp_get_attachment_url($post->ID) . '" target="_blank">Actual File</a>';
    return $actions;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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