Force Download PDF in WordPress

WordPress already enables you to to download PDFs and other file types simply by changing the attachment link in the media library. Like so: Attachment details for pdf file in media library But what if you want to force it to download instead of opening it up in another window? This is a quick one now so pay attention. in an .htaccess file:

or via a php function:

Just a note. This is not particularly WordPress-y and can be done with all sorts of file types not exclusively .pdf’s If you want to style it like a button just give the link a class of button and use css to style accordingly.

21 comments on “Force Download PDF in WordPress

  1. I want to download the files of the types of force please provide me exact site from where I can download it in very explanation way file should be

  2. Which php file does that php function go in?
    I am using wordpress with godaddy. I can access many php files on the appearance:editor page (many php files except wp-admin that is). Although the .htaccess solution looks easier, I have no luck finding that even after many web searches. Thanks! Andrew

  3. I like It.. Bt i hv problem that, I di custom link and provide URL of PDF file. So how i do for download the PDF file on click rather open in other tab…Plz help.

  4. Hi, thank you for posting this, I tried the .htaccess code and it works in every browser I’ve tested (Chrome, FireFox, Opera, Safari, Internet Explorer) but it doesn’t work with Microsoft Edge, of course. Do you know of a fix for this? Anybody else have this problem?

    Thanks!

    • Hi John, Its up to the browser how it handles the ability to download a file automatically and I don’t know a way around this for Edge. I did find this by way of explanation link to Microsoft answers. Would you be so kind as to post a comment if you find a good solution to this? I’ll do the same if I come across anything.

  5. Thank you for this: just what I needed. BTW the WP interface has changed slightly, and you just have to link to the library item. Do not select “Open in other tab” as this seems to bypass the download instruction and force the PDF to open in browser.

  6. Hi, just stumbled upon this as I was looking for answers to the same question. Just wanted to know whether all of the following was meant to be added:
    AddType application/octet-stream .pdf
    Thanking you for your response in advance!

    • Hi Phil, yes, you can add that (just copy/paste the whole line) to your .htaccess file which is usually at the root of your site. You can also try the above suggestion and simply not select the “Open in other tab” when you link the file via the backend.

  7. I’ve used your PHP in a simple plugin but it seems to be applying to all links on the site – instead of navigating to a new page, a file is downloaded; instead of activating a plugin, a file is downloaded.

    Am I missing something?

    • Hi Jon, sorry to take so long to get back to you. In this case you would set the variable to the file you wish to be downloaded $f=filename.pdf or you could loop through several files using PHP’s pathinfo function:
      foreach($files as $file) {
      pathinfo($file, PATHINFO_EXTENSION);
      }

Leave a Reply

Your email address will not be published. Required fields are marked *

Click to view allowed tags

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>