Open WordPress Comment Links in New Tab/Window (add target=”_blank”)

Posted by on Sep 2, 2013 in WordPress ·

The comment feature of WordPress really makes it possible to interact with users. Comments are why users take more interest in a blog. Many times quality discussions happen through the comments. So comments are very important for a blog.

But one of the problems with the comments is that all the links (URLs) in the comments open in the same tab. This diverts the user’s mind away from the actual post and increases the bounce rate. Here is the solution to this problem. We can open all the comment links in a new tab or window in WordPress. The task is very simple and really helpful.

Open Comment Links in New Tab WordPress

Add target=”_blank” to comment links

By adding target="blank"聽to the comment links and author URL, we can open them in new window. To achieve this, we will add filters to two WordPress hooks – get_comment_author_link and聽comment_text.

Method 1 : Using Theme

You can add the following code to your theme’s functions.php file. Make sure you do not add it between other existing functions there.

function comment_links_in_new_tab($text) {
$return = str_replace('<a', '<a target="_blank"', $text);
return $return;
}
add_filter('get_comment_author_link', 'comment_links_in_new_tab');
add_filter('comment_text', 'comment_links_in_new_tab');

Method 2 : Plugin

There is a drawback of the above method. When you will update your theme, you lose all the code you added. But if you implement it with a plugin, you will have it forever.

To create a plugin, first create a new folder in聽WP-content -> plugins. Let’s name it “comment-links-in-new-tab”.聽Then create a new PHP file in it – say, “comment-links-in-new-tab.php”.
(You can give any name to the folder and the file. Just make sure it’s unique.)

Then add the following code to the PHP file :-

<?php
/*
Plugin Name: Open Comment Links in New Tab
Plugin URI: http://www.tricksbag.com/
Description: This plugin adds target="blank" to the links in comments and author URL. So all the links open in new tab.
Version: 1.0
Author: Animish Andraskar
Author URI: http://www.tricksbag.com/author/animish/
*/

function comment_links_in_new_tab($text) {
$return = str_replace('<a', '<a target="_blank"', $text);
return $return;
}
add_filter('get_comment_author_link', 'comment_links_in_new_tab');
add_filter('comment_text', 'comment_links_in_new_tab');

/* End of plugin */ ?>

Done! Now all the links in comments and author URL will open in new tab.

As usual, don’t forget to leave a comment and follow us on Facebook and G+.

Last updated on : December 24, 2013.

21 Comments on “Open WordPress Comment Links in New Tab/Window (add target=”_blank”)”

  1. Well done! I had been modifying comment-template.php, but wanted something that wouldn’t be lost when upgrading WordPress; this is just what I needed. Thank you

  2. The functions.php filter is exactly what I was looking for, thank you!

    There’s never a good reason to edit the core wordpress files because there’s always someone out there like you who is kind enough to share details on how to achieve the result you want directly in your theme.

    I wish this was the #1 result that popped up in my search for “change comments author link output wordpress.” Thanks again!

  3. Really helpful. Just one thing: isn’t possible to put it at the end after href and nofollow? Anyway, this is actually better than any other solution I’ve found which overwrite your posts, while this is just a toggle that can be turned on/off.

    1. Hi Edward!

      It is not possible to put the target after href or rel. But the order doesn’t matter at all.
      So, <a target="_blank" href="http://domainname.com" rel="nofollow">Click here</a> will always work exactly as <a href="http://domainname.com" target="_blank" rel="nofollow">Click here</a>.

  4. Hi Animish, Thanks for such a clear answer! It had been bothering me that comments authors links meant leaving my site, but wanted them to have a link. so nice to put in a search for an answer and have your post come up at the top of the list! Thanks again!

  5. Many thanks Animish for the excellent tutorial and the plugin itself. I learnt something from your article and now have the functionality I needed. And it was simple!

  6. I have tried both the ways and can’t seem to get this to work. Is there any other plugins that this might not be compatible with that I need to know about?

    1. I’ve been playing around with this some more and the links within the comment are opening in a new window but the author URL link is still opening in the same window. Any ideas?

      1. It is great to see that the trick is working for you. This trick is only for links inside comment text. If you want to open author links in new tab, you will have to tweak your theme files. You should contact your theme provider to know the file/s which render author name and URL in the front end.

Leave a Reply