Categories
Uncategorised

Open PDF in new tabs – WordPress

To force WordPress to open all PDFs in a new tab, add the following to your theme-functions.php.

function pdf_target_blank(){ ?>
<script type="text/javascript">
  jQuery(document).ready( function(){
      jQuery('a[href$=".pdf"]').attr('target', '_blank');
  } );
</script>

<?php }
add_action( 'wp_print_footer_scripts', 'pdf_target_blank' );

This code can be added as just the Script into the header.php of your site, though in certain interfaces it won’t always work.

Why would you use this?

We use¬†WordPress as a College intranet that is then rendered as an iframe inside their management system SEQTA. This interface means that certain jQuery and Javascript won’t work as expected, specifically linking to pdfs.