Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to add a button (or rather, a link formatted to look like a button) in the utilities area somewhere above the main navigation menu of a WordPress-based business site (that is, not in the body of a post or page). The purpose of the link is to enable the website visitors to download the company's annual report to their computers, either from media library, or from some other directory on the server.

I found some information among earlier WordPress Answers, but still couldn't figure out the best way to do what I need to be done.

Just to explain what I mean: I'm not asking how to place a link into a page. What I want to learn is how to force a file download onto a user's computer. Apparently, there are plugins that allow you to do that, but they all end up with a link in the body of the post. What I want is a link in the website's header.

Would be grateful for assistance!

share|improve this question
1  
If you are referring to the Menubar at the top of the page when you are logged in as an Admin, that isn't viewable to everyone. You have to be logged in to see that utilities bar. –  ckpepper02 May 23 '13 at 18:58
    
Nope, I', referring to a bunch of small links above the main navigation menu that are commonly called "utilities" in web-speak. ;-) –  Astoria May 24 '13 at 11:19
    
What Theme are you using? Where exactly in the markup do you want such a link? Will this download link/button be the same throughout the entire site, or context-dependent? –  Chip Bennett May 24 '13 at 14:51
    
Actually, reading your comment below, I believe this question is off topic. How to force the browser to download a resource from a link is not WordPress-specific. –  Chip Bennett May 24 '13 at 14:53
add comment

2 Answers

up vote 2 down vote accepted

The pdf is itself an attachment post so in wordpress template hierarchy we can create a template named pdf.php Then you can write following code in it which force download the pdf file.

<?php  

if (have_posts()) :
the_post();

$pdf_title = $post->post_title;
$pdf_src = get_attached_file($post->ID );
$bytes =  filesize( $pdf_src );

 header("Pragma: public"); // required
 header("Expires: 0");
 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
 header("Cache-Control: private",false); // required for certain browsers 
 header("Content-Type: application/pdf");
 header("Content-Disposition: attachment; filename=\"".$pdf_title."\";" );
 header("Content-Transfer-Encoding: binary");
 header("Content-Length: ".$bytes);
 ob_clean();
 flush();
 readfile("$pdf_src");


endif;

While linking the button link to its attachment page( not the file url) and the job will be done. Good luck.

share|improve this answer
    
Thank you, @Dipesh, let me try what you've suggested! –  Astoria May 31 '13 at 18:48
    
I have a question, @Dipesh. When you say "the pdf is itself an attachment post", do you mean that in order for your pdf.php script to work, I should include pdf file as media into a page, and then use pdf.php as the template for that page? –  Astoria Jun 5 '13 at 12:29
    
You're right, and the link to the the attachment page is our dowload link(above is a template for the same) –  Dipesh Kc Jun 5 '13 at 15:11
    
thank you, @Dipesh –  Astoria Jun 5 '13 at 16:16
add comment

Does your theme allows you to display a custom content above the menu area? Some themes give you a input box or widget area where you can display markup like you described in to top right corner.

Try reading documentation of a theme or pork around theme settings and visit Widget area to see if you have that ability.

Otherwise you can use jQuery to dynamically insert the markup anywhere on the website if you are comfortable with it:

http://api.jquery.com/prepend/ http://api.jquery.com/html/

share|improve this answer
    
Thank you, @Elisha. The problem is not how to display a link - I can just stick it into html code for the website's header. My question is about how to force the download. I'll edit the question to make it a little clearer. –  Astoria May 24 '13 at 11:20
1  
I see, so this is more .htaccess question than a WordPress. I happened to solve the same issue before so I'll just paste the .htaccess snippet that worked for me: <FilesMatch "\.(pdf)"> Header set Content-Disposition attachment </FilesMatch> –  Elisha Terada May 24 '13 at 15:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.