[Resolved] Assign Global Target to File Type PDF

Home Forums Support [Resolved] Assign Global Target to File Type PDF

Home Forums Support Assign Global Target to File Type PDF

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #202375
    Terry

    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

    #202383
    Tom
    Lead Developer
    Lead Developer

    That wouldn’t be added into a hook – you would have to use one of the methods here: https://generatepress.com/knowledgebase/adding-php-functions/

    #203155
    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

    #203156
    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');
    
    #203209
    Tom
    Lead Developer
    Lead Developer

    Thanks for sharing your code! 🙂

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.