How To Make Comment Author Links (URLs) Open In New Windows In WordPress

It’s nice to allow visitors to comment on your website, but not so nice when your visitors click on links to a comment authors website and you lose your traffic. Obviously you can get around this by adding target=”_blank” to the hyperlink attributes so the url opens in a new window, but it’s not obvious how to do this in WordPress.

So, here’s some code you can put in your functions.php file (which is part of your WordPress theme), so make comment author links open in a new window:

function comment_author_link_window() {
global $comment;
$url    = get_comment_author_url();
$author = get_comment_author();
if ( empty( $url ) || 'http://' == $url )
$return = $author;
$return = "<a href='$url' rel='nofollow' target='_blank'>$author</a>";
return $return;
add_filter('get_comment_author_link', 'comment_author_link_window');

As you can see, it puts a WordPress filter on the get_comment_author_link function and effectively swaps it for the new function called get_comment_author_link. This new function then takes the authors url and adds the rel=”nofollow” tag on so search engines won’t credit spammers with back-links. It also adds the target=”_blank” tag so comment authors links open in new windows and you don’t lose your visitor traffic.

If you’re a little unsure where to put this code, do the following:

  1. Log into the WordPress dashboard
  2. Click Appearance
  3. Editor
  4. Select the theme you are using from the dropdown in the top right of the screen
  5. Click on Theme Functions (Functions.php) on the right hand side
  6. Scroll to the bottom of the code and paste the above code

I would recommend doing this in every WordPress theme as standard so you won’t lose visitors to comment authors links and if you’re not allowing comments on the website, the code simply will not be called.

VN:F [1.9.22_1171]
Rating: 9.0/10 (2 votes cast)
How To Make Comment Author Links (URLs) Open In New Windows In WordPress, 9.0 out of 10 based on 2 ratings

Leave a comment

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