如果您不介意强制下载所有PDF附件,那么您可以使用以下内容:
<?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[\'basedir\'], $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;
?>
将上述代码放入名为pdf的文件中。当前主题文件夹中的php。而不是直接链接到pdf(http://example.com/wp-content/uploads/2011/01/Guide-to-Owning-a-Listed-Building.pdf),链接到附件URL:(http://example.com/help-and-advice/attachment/guide-to-owning-a-listed-building/)
按照上面的操作,您可以编辑代码来执行其他奇特的操作,例如跟踪下载,并添加某种级别的身份验证,同时保护PDF的实际位置。