PDF将以mime类型保存application/pdf
, 所以如果你的主题有application.php 或pdf.php 强制下载的模板文件(或者如果您在attachment.php 模板),您可以强制下载。
A.pdf.php 在主题中这样构建的文件应该可以做到:
<?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;
?>
(编辑:我应该注意的是,为了让它工作,当你通过媒体上传器将文件插入帖子时,你必须选择
Post URL 而不是
File URL 在插入帖子之前,请在链接URL字段中。指向文件名的链接将遵循浏览器的首选项,但通过链接到WP post链接,您可以控制其行为。)