Posts Tagged ‘functions’

How to open the comments author link in a new window

Posted on: 1 Comment

On most wordpress stock themes, you will find that when the user clicks on the comment author link. It opens directly in the same page, taking the user away from the site.

Ideally when the user clicks, it should open the commenters site in a new window. After all, they are being directed to a new website.

Amazingly it seems most suggestions you will find on Google for this is to modify the wp includes core files. This is never ideal and the better approach is to use WordPress’s filters instead. After all, this is exactly what they are for!

What we are going to do is add a new function that includes the link structure how we want it, using the original structure as a base, then run a filter on the “get_comment_author_link” to change it with our new function.

So open up your themes functions.php file and somewhere at the bottom, add the following. I have added comments on each section of the function so you can easily understand whats happening.

function comment_author_add_blank() {
	global $comment;
	//get url
	$url    = get_comment_author_url();
	//get author
	$author = get_comment_author();
	// check if user added a url or not
    // if no url, return author
	if ( empty( $url ) || 'http://' == $url )
		$return = $author;
	// if has url, return url with new format and _blank
		$return = "<a href='$url' rel='external nofollow' target='_blank'>$author</a>";
	return $return;
// now run the filter on our new function
add_filter('get_comment_author_link', 'comment_author_add_blank');

Thats all folks!