Add target blank to user link in recent comments widget

Open author link in new tab in wordpress default recent comments widget.

I just discovered it today itself on webdeveloeprswall. I’ve added a recent comments widget in my sidebar. The widget is a default wordpress widget, many of us use it. But when you click on the author link in the widget(sidebar) the commentor’s url (whatever he/she has provided) open in the same window!! Taking visitors away from your own website.

I immediately felt the need to add a target="_blank" attribute to those links. So here’s the small code snippet i came up with. Just add the below lines of code in your theme’s functions.php file:

<?php 
/**
 * http://webdeveloperswall.com/wordpress/add-target-blank-to-user-link-in-recent-comments-widget
**/
function add_blank_get_comment_author_link($link){
	$rep_count = 1;
	$new_ret = str_replace("<a", "<a target='_blank'", $link, $rep_count);
	return $new_ret;
}
add_filter('get_comment_author_link', 'add_blank_get_comment_author_link', 10, 2);
?>

and now the author links from recent comments open in new tab, hurray!! 🙂

Hope you find it useful. Cheers

Published by

Chandan Chaudhary

Chandan is a WordPress guy. A programmer at heart. He loves building community sites with WordPress and BuddyPress.

4 thoughts on “Add target blank to user link in recent comments widget”

  1. By the end of the 17th century, a more clinical and
    scientific approach to health, based on actual observation, began to
    appear. Use less sugar, eat less white flour and processed foods to allow the body time to
    heal and re-balance itself. Find some Unicorn cupcake
    toppers or an edible image of one of these magical horned horses to put on your birthday cake.

  2. First of all I would like to say awesome blog! I had a quick question which I’d like to ask if you don’t mind. I was interested to find out how you center yourself and clear your head prior to writing. I have had trouble clearing my mind in getting my thoughts out there. I truly do enjoy writing but it just seems like the first 10 to 15 minutes are usually wasted just trying to figure out how to begin. Any ideas or hints? Cheers!

  3. Aaaaaah!  I’ve been looking for that target_blank solution for hours (or days). I’ve tried pieces of codes given on the net; nothing worked EXCEPT YOUR SOLUTION ! TTTTTTTThank you!!!

Leave a Reply

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