WordPress is a great Content Management System, not just for bloggers …
One of the downsides I have run into is that links in comments automatically get reformatted and the HTML “target=_blank” gets stripped away so a link in a comment will always open in the same window as you website. Personally I prefer such a link to open in a new window …
So in this article a simple piece of code to make links in WordPress comments automatically open in a new window or a new tab by automatically adding “target=_blank” to links in comments.
Ad Blocking Detected
Please consider disabling your ad blocker for our website.
We rely on these ads to be able to run our website.
You can of course support us in other ways (see Support Us on the left).
Modify Comments to have links in comments open in a new window or tab
For this we are going add a filter in the functions.php file of your theme.
I prefer this kind of an approach over a plugin, since I’m fully in control of what’s happening … you might understand this reasoning when you’ve used plugins before that were abandoned after a while …
Add the following code to your functions.php of your theme:
return str_replace('<a', '<a target="_blank"', $text);
This does two things:
1. Creates a function
t4a_comment_links_in_new_window which takes a string and replaces every ocurance of
<a (from the HTML A HFER tag) and replaces it with
2. Add this function to the
comment_text filter so that it’s being called when a comment is being filtered.
The result is that any link in a comment will now open in a new window or a new tab.
Please note that this can also be achieved with plugins like for example No Follow for External Link (the name is a bit misleading, bt it does the trick).
Your support is very much appreciated, and can be as easy as sharing a link to my website with others, or on social media.
Support can also be done by sponsoring me, and even that can be free (e.g. shop at Amazon).
Any funds received from your support will be used for web-hosting expenses, project hardware and software, coffee, etc.
Thank you very much for those that have shown support already!
It's truly amazing to see that folks like my articles and small applications.
Please note that clicking affiliate links, like the ones from Amazon, may result in a small commission for us - which we highly appreciate as well.
There are 4 comments. You can read them below.
You can post your own comments by using the form below, or reply to existing comments by using the "Reply" button.
Hi, I just wanted to thank you for this solution to open links in comments in a new window or tab. It was quick and easy to complete. Keep up the great work. Thanks JJ
Thanks JJ for taking the time to post a Thank-You – it’s much appreciated and glad to hear it was helpful for you!
hey thanks for this! It was exactly what we needed :)
Awesome! Thanks Milly for taking the time to post a thank-you – it is much appreciated