WordPress Plugin: PDFs Open in New Tab

I had a client recently where I had to import over 150 pages into their site in order to get the moved over to WordPress. I didn’t have access to the database as it was a proprietary CMS they were using, so I ended up having to use HTTrack to take an offsite copy of the entire site. Needless to say, I ended up writing a few tools to import everything so I wouldn’t have to do it manually. I uploaded all of the html files to the server, sideloaded all of the images, set the first attachment as the featured image, and was done in probably 1/10th of the time it would have taken to go through them all one by one and copy and paste. Then I realized that all of the attached PDF files opened in the same tab. I could handle this one of three ways:

  1. Find and replace in the database
  2. Javascript or jQuery to append the target attribute
  3. Use WordPress hooks and some fancy Regex

I opted for number 3, as it would be basically transparent, and wrote a quick plugin. This can easily be adapted to work with any extension of attachment. Here we go:

 

One Comment on “WordPress Plugin: PDFs Open in New Tab”

Leave a Reply

Your email address will not be published. Required fields are marked *