Inserting a Download Link in the Quick Edit Actions of the Media Library?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.'" rel="nofollow noreferrer noopener">Download</a>';

    return $actions;    
}

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

Method 2

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) . '" rel="nofollow noreferrer noopener" download="' . wp_get_attachment_url($post->ID) . '" target="_blank">Actual File</a>';
    return $actions;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x