0

Seeing as I have an audio player on my page I don't want safari to open the download link in a new tab with another player, I want the mp3 file to download to their computer.

My download link is located on a custom post-type index page:

<a href="<?php echo $audio_track_url; ?>?action=download_track" download>
    <?php _e('Download', 'txt-domain' ); ?>
</a>

When users click this link the file is downloaded - Using the "download" attribute works fine on most browsers but not Safari. Currently, to work around this I am trying to modify the headers like this:

//force mp3 file download
add_action( 'post_type_download_track', 'force_mp3_download');
function force_mp3_download() {
    if(!is_admin()){
        header('Content-Type: application/mp3');
        header('Content-Disposition: attachment; filename=example.mp3');
        header('Pragma: no-cache');   
    }
}

In this question the href in the tag is appended admin_post.php?action=print.csv and the action used is admin_post_print.csv but I'm not sure which action to use in my case?

  • See Export data as CSV in back end with proper HTTP headers for how to force a download. – fuxia Dec 8 '16 at 14:08
  • Thanks for the link but I have no idea how to apply that to my situation – Ralphonz Dec 8 '16 at 16:24
  • Adjust the Content-Type, and send the two other headers as they are. – fuxia Dec 8 '16 at 18:02
  • No i understood that part - i'm stuck on which action I should use instead of add_action( 'admin_post_print.csv', 'print_csv' ); if my action is ?action=download_track and I'm on the frontend index screen for a custom post type? – Ralphonz Dec 8 '16 at 22:44
  • That depends on the way you are including the files. If you are using made-up URLs, template_redirect seems fine. – fuxia Dec 8 '16 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.