Shortcode: Force File Download
-
Is there any way to force a file (.pdf) included in the gallery shortcode to download rather than display in a browser’s plugin?
-
Thanks for this interesting question. A quick search turns up several helpful articles (see below). The one I found most useful is:
Forcing PDF to Download through htaccess – Link removed by mods
I created a small test file along the lines suggested and got a “Do you want to open or save … ” dialog box when I accessed the PHP file from my browser. Here is the “ForceDownload.php” file I put in the root directory of my site:
<?php if ( isset( $_REQUEST['download_file'] ) ) { if ( isset( $_REQUEST['download_name'] ) ) $name = $_REQUEST['download_name']; else $name = 'document'; header( 'Content-disposition: attachment; filename=' . $name ); header( 'Content-type: application/pdf' ); readfile( $_REQUEST['download_file'] ); } else { echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'; echo '<html xmlns="http://www.w3.org/1999/xhtml">'; echo '<head>'; echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; echo '<title>File Not Found</title>'; echo '</head>'; echo ''; echo '<body>'; echo 'File Not Found'; echo '</body>'; echo '</html> '; } ?>and here is the
[mla_gallery]I tested with:[mla_gallery post_parent=all post_mime_type="application/pdf" link=file mla_caption="{+base_dir+}/{+base_file+}" mla_link_href='/ForceDownload.php?download_file={+base_dir+}/{+base_file+}&download_name="{+file+}"']You could add a MIME type argument to generalize this to other file types, using the
{+mime_type+}substitution parameter.The other approach suggested is to modify the .htaccess file to add the Content-disposition header to all PDF files; this seems less useful.
There are also some comments about the varying degree of browser support for this HTPP header. Have a look through these links, pick an approach and let me know how it works out.
More readings
force users to download pdf versus opening in browsers
Force Files to Download (Not Open in Browser)
Force Files to Download Instead of Showing Up in the Browser
How To Use PHP to Force a File Download
Content-Disposition header – forcing SaveAs in browsers
Uses of content-disposition in an HTTP response header
The Content-Disposition Saga: Controlling the suggested file name in the Browser’s Save As Dialog
Awesome. Thanks for the resources. i’ll check them out in time.
hello, David (and UaMV).
I’ve researched this issue a bit and read through all the links David provided in his reply above.
seems there’s yet another way to approach this matter via HTML5 (using the download attribute) as described in this post:
http://svarden.se/blog/2013-04-22-right-click-and-save-as/
I’m no web developer so please forgive my ignorance but my sense is the HTML5 download attribute should be do-able somehow using your
mla_link_attributesparameter.Is it possible to nest ‘Attachment-specific substitution parameters’ inside ‘Gallery Display Content parameters?’
If so, might something like this work to force download of attachment links in any HTML5 compliant browser:
[mla_gallery mla_link_attributes='download="{+file+}.{+mime_type+}"]Alternatively, if it’s not possible to nest parameters like that is there any way to make use of the ‘mla_link_attributes’ in Markup Templates » Item?
Am I totally of the mark?
Thanks for your contribution and for posting the link to more details. Your
mla_link_attributessuggestion is a good one, but you don’t need the.{+mime_type+}portion of the value.I did a quick test and the
downloadattribute works in Firefox and Chrome, but sadly not in Internet Explorer 10 (for me, anyway).I created an
[mla_gallery]shortcode as follows:[mla_gallery size=icon link=file post_mime_type=application/pdf ids=18 mla_link_attributes='download="{+file+}"']In Firefox I got a download dialog box when I clicked on the PDF icon in my gallery. In Chrome, the file was immediately downloaded and I got a “keep or discard” choice.
In Internet Explorer 10, the file opened up in the Adobe Reader browser plugin, as if no
downloadattribute was present. It’s possible that changing the HTML Document header or some other coding change would fix this issue, but I haven’t researched it.In the example you see that I simply used
{+file+}to supply a name for the download. This substitution parameter has the name and extension parts of the file specification. Mime type and file extension are not the same thing. For example, a PDF document has a Mime Type of “application/pdf” as my example shows.Thanks for another alternative to force downloads. When HTML 5 is widely adopted it will make life easier!
okay totally noob question, where does one write and save short codes to?
ericgfx01,
If you are asking what shortcodes are and how to use them, start with this definition from the WordPress.com support site:
What is a shortcode?
A shortcode is a WordPress-specific code that lets you do nifty things with very little effort. Shortcodes can embed files or create objects that would normally require lots of complicated, ugly code in just one line. Shortcode = shortcut.The WordPress Codex has this page:
Basically, you add a shortcode to the content of your post or page when you want to do something like display a gallery of images from your Media Library.
If I have not understood your question, give me more details and I can be more specific.
The topic ‘Shortcode: Force File Download’ is closed to new replies.