Need quick help - how to make link download a PDF, and not have open it in a webpage - Wordpress

First thing first, I am a total beginner in Wordpress.

The issue I am having is, I set up a link to go to PDF (using the Wordpress editor) and when the user
clicks the PDF link, it opens in a webpage. I would prefer they get a download/open dialogue instead.

Is there an easy way for a non-coder to accomplish this?

Your help is greatly appreciated,
HappyT
TheHappyTechAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jason C. LevineNo oneCommented:
This isn't a WordPress issue, it's a client browser issue.  Because I (as the client) choose how to handle PDF files in my browser, I can default them to save or open as I choose.  All you can do to affect this is to add lines to the .htaccess file to attempt to force a download:

<Files *.pdf>
    Header set Content-Disposition attachment
</Files>

or

AddType application/octet-stream .pdf

Those might force most browsers to download, but I still think that my preferences override it.

Alternately, just zip the PDF and upload the zip file.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NerdsOfTechTechnology ScientistCommented:
In other words you need FTP access to the .htaccess file of the directory you are putting the pdf file in for download to force the PDF download.

Otherwise you will want to put the PDF file into a compressed folder (zip file) and link the zip file (a file type that almost always prompts the user to download the file) ----and it saves broadband too!
0
Ray PaseurCommented:
The "non-coder" part may make this question hard.  It might be helpful to enlist the assistance of a "coder" who understands the pressure points.  Perhaps there is a WordPress user group in your area?

Here is how I "force a download."  It's not foolproof, but it has worked well for me.
<?php // demo/force_download.php

/**
 * Show how to force a file download
 *
 * Choose a file to download, either via a hard-coded file path
 * or via a URL parameter like this
 *
 * path/to/demo/force_download.php?url=path/to/file.txt
 *
 * If you wanted to use the URL parameter, you might want to
 * (1) Secure the script with password protection
 * (2) replace the $url variable with something like this:
 * $url = $_GET['url'];
 *
 * If you use the URL parameter, be careful that the script
 * does not expose sensitive data on your server!
 */
error_reporting(E_ALL);


// WHAT FILE DO WE WANT TO DOWNLOAD?
$url = "http://www.IcoNoun.com/demo/short_text_file.txt";


// USE CASE
force_download($url);


// FUNCTION TO FORCE A DOWNLOAD FROM A FILE
function force_download($url, $filename=NULL)
{
    // GET THE DOWNLOAD FILE NAME
    if (empty($filename)) $filename = basename($url);

    // GET LENGTH AND FILE RESOURCE POINTER
    $hdr = get_headers($url, TRUE);
    $len = trim($hdr['Content-Length']);
    $fpr = fopen($url,'rb');

    // ON SUCCESS
    if ($fpr)
    {
        // THESE HEADERS ARE USED ON ALL BROWSERS
        header("Content-Type: application-x/force-download");
        header("Content-Disposition: attachment; filename=$filename");
        header("Content-length: $len");
        header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
        header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");

        // THIS HEADER MUST BE OMITTED FOR IE 6+
        if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
        {
            header("Cache-Control: no-cache, must-revalidate");
        }

        // THIS IS THE LAST HEADER
        header("Pragma: no-cache");

        // FLUSH THE HEADERS TO THE BROWSER
        flush();

        // WRITE THE FILE
        fpassthru($fpr);
    }

    // ERROR
    else
    {
        trigger_error("Unable to open $url", E_USER_ERROR);
    }
}

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Jason C. LevineNo oneCommented:
Ray,

Does every browser respect application-x/force-download?
0
Jason C. LevineNo oneCommented:
and it saves broadband too!

Eh, not really.  A PDF is already compressed.  You don't gain any real savings in file size by zipping it.  It just forces the download in a kludgy way.
0
NerdsOfTechTechnology ScientistCommented:
You're right: Putting it into zip triples the work (packing it, uploading it, and having the user unpack it). PDF with force download is definitely the way to go. I second Ray's idea.


Yes, every browser will respect the forced download.
0
Jason C. LevineNo oneCommented:
So if all browsers respect it, adding this line to .htaccess:

AddType application-x/force-download .pdf

replaces my original answer.
0
TheHappyTechAuthor Commented:
Ok, I will give it a try. Thank you everyone!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.