Page 1 of 1

WordPress – Open links in comments in a new Window or Tab

WordPress – Open links in comments in a new Window or Tab
   4

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.




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:


1
2
3
4
5
6
function t4a_comment_links_in_new_window($text)
{
    return str_replace('<a', '<a target="_blank"', $text);
}

add_filter('comment_text', 't4a_comment_links_in_new_window');

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 <a target="_blank".

2. Add this function to thecomment_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).

Support Us ...


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.

Comments


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.

  • Apr 11, 2019 - 6:08 PM - JJ Smith - Author: Comment Link

    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

    Reply

    JJ Smith

    • Apr 12, 2019 - 4:37 AM - hans - Author: Comment Link

      Thanks JJ for taking the time to post a Thank-You – it’s much appreciated and glad to hear it was helpful for you! 

      Reply

      hans

  • Sep 18, 2020 - 12:02 PM - Milly Comment Link

    hey thanks for this! It was exactly what we needed :)

    Reply

    Milly

    • Sep 19, 2020 - 4:36 AM - Hans - Author: Comment Link

      Awesome! Thanks Milly for taking the time to post a thank-you – it is much appreciated 

      Reply

      Hans



Your Comment …

Do not post large files here (like source codes, log files or config files). Please use the Forum for that purpose.

Please share:
*
*
Notify me about new comments (email).
       You can also use your RSS reader to track comments.


Tweaking4All uses the free Gravatar service for Avatar display.