How to open links in a new tab only for custom post types
Recently, I’ve been building a custom project where users will be able to download a library of resources. It wouldn’t be an ideal management experience to check/tick the open in new tab/window option when adding links to all the resources. There will be hundreds! But at the same time, I don’t want to change the default link behavior, because these resources will be (so far) the only thing that I will want opened in a new window.
There’s of course not a single solution, but a couple ways to achieve this. I opted for adding a filter to my template file for the custom post type. I could have added this to the functions.php file, but that would add to the server wait time. I’m told if it’s added to the template file itself, it saves a few milliseconds of server processing the entire functions file each page load unless you have server caching. I haven’t tested this myself though. I also could have added a short jquery snippet, but again, why load another file client side adding wait time when the site is going to be serving up tons of documents and potentially have a lot of traffic.
In my case, these resource links will very likely never need to NOT open in a new window. But I will need to add even tracking to track how users are using the site and which resources they’re downloading. Since I do not have any download links on the archive view, I only needed this for the single view of the post type.
If you need help altering this for your case, let me know and I’ll be happy to help.
Leave a Reply
Want to join the discussion?Feel free to contribute!