Site logo

Archived topic

Assign Global Target to File Type PDF

4 replies · Started by Terry on June 15, 2016

Viewing posts 1–5 of 5

I have to connect a boatload of PDFs as posts. I want them to open in a new browser window, and don't see a way to easily do that using the 'link' post type. So I stumbled across this tip online:

https://lukodp.wordpress.com/2013/11/04/adding-target-to-pdf-links-in-wordpress/

that gives a solution which adds code to the functions.php file.

Does it have to go in that page? Or can it be added as a hook? If it CAN be added as a hook, can you tell me which one I should put it into?

Many thanks!

Terry

Thanks, Tom. I used the Code Snippets plugin to get this working. Brilliant! Love that I don't have to add code to functions.php anymore!

Terry

P.S. If anyone wants to use this, the code as copied from the URL above doesn't work because it uses curly quotes. Try this instead:

/*adding target to pdf attachment*/

function add_target() {
?>
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function($){
$(".entry-content a[href$='pdf']").attr('target','_blank'); /*you can use other target attribute beside _blank*/
});
</script>
<?php
}
add_action('wp_footer','add_target');

Thanks for sharing your code! :)

This archived topic is closed to new replies.