WordPress already enables you to to download PDFs and other file types simply by changing the attachment link in the media library. Like so: 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.
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
I’m afraid I don’t understand your question.
Thank you very much the trick with the .htaccess file worked perfectly best post so far!
You are very welcome. Glad you found it useful!
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
Hi Andrew, you would have to either create a simple plugin (recommended) or put it in your theme’s functions.php file. The .htaccess is perhaps easier but there are some good arguments against using .htaccess https://haydenjames.io/disable-htaccess-wordpress-performance/
Thank you, a life save this tricks !!!
How can i implement this on a IIS7 installation of WordPress?
I’m afraid I don’t know the particularities of an IIS7 installation. Have you tried using the code in header() function? http://php.net/manual/en/function.header.php
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.
Hi Kiran this is something that has to be handled directly in the browser. In Chrome, for example, you can diable the PDF viewer in your settings chrome://settings/content/pdfDocuments. You cannot, to my knowledge, impose this upon some your end user.
Perfect simple trick, really helpful!
nice post!
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.
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.
Thanks Jon, Good to know! Nice to have the option without going to all the fuss.
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.
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);
}