Question: How do I add a link to a PDF in the content, so that it downloads rather than opens in the browser windows? (I've uploaded the PDF to the media library and can insert the link but can't find an option to prevent it opening in the browser window).

Thanks,

Lucy

Answer: 
A PDF will be saved with mime-type of `application/pdf`, so if your theme has an **application.php** or **pdf.php** template file that forces download (or if you check for mime-type in your **attachment.php** template), you can force a download. 

A **pdf.php** file built like this in your theme should do the trick:

```
<?php  if (have_posts()) : while (have_posts()) : the_post();

$pdf_title = $post->post_title;
$uploads_dir = wp_upload_dir();
$attachment_src = get_post_meta( $post->ID, '_wp_attached_file', true );
$pdf_src = path_join( $uploads_dir['path'], $attachment_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: ".filesize($pdf_src));
 ob_clean();
 flush();
 readfile("$pdf_src");

endwhile; endif;

?>

```

(Edit: I should note that in order for this to work, when you insert the file into your post through the media uploader, you have to select **Post URL** rather than **File URL** in the *Link URL* field before inserting into your post. A link to a filename will follow the browser's preferences, but by linking to the WP post link, you can control its behaviour.)
Just use the a tag's download attribute: <https://www.w3schools.com/tags/att_a_download.asp>