• Skip to primary navigation
  • Skip to content
Eyks

Eyks

We make things that make a difference

  • About
  • Concern
  • Contact
  • Blog
  • Show Search
Hide Search

Open PDF in new tabs – WordPress

January 14, 2016 By Skye Moroney

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.

Filed Under: Uncategorised Tagged With: PDFs, SEQTA, Wordpress

Reader Interactions

Concern.net.au

Eyks

Copyright © 2021 Eyks 2019