How to Avoid Losing Visitors Due to Comment Links

Note: The code in this article was updated and tested to work as of 2015-09-01.

If you run a WordPress-powered website, ask yourself, how many users leave your site by following the links left by commenters? The answer will no doubt vary greatly site to site, but if your users are leaving your site prematurely and forgetting to come back, then you need to improve that situation straightaway.

And it’s easy to do! What we’ll do is add a small snippet of code to your site which will alter the links to instead open in a new tab (or window, if tabs are unavailable), ensuring your site remains open in the user’s browser. Links which point to the current site’s address (as determined by home_url() will not open a new tab). 

Simply open your Thesis 1.8.x custom_functions.php or your site’s functionality plugin and paste in the following bit of code:

 * Comment author links open in new windows
function custom_pop_open_external_commenter_links ( $link ) {
	if ( ! strstr( $link, get_home_url() ) ) {
		return str_replace( 'rel=\'', 'target=\'_blank\' rel=\'', $link );
	} else {
		return $link;
add_filter( 'get_comment_author_link', 'custom_pop_open_external_commenter_links' );

If you want all comment author links to open in new windows, simplify the code to this:

 * Comment author links open in new windows
function custom_pop_open_external_commenter_links ( $link ) {
	return str_replace( 'rel=\'', 'target=\'_blank\' rel=\'', $link );
add_filter( 'get_comment_author_link', 'custom_pop_open_external_commenter_links' );

Also, I should point out that this code invalidates Thesis’ XHTML strict markup due to that particular standard not recognizing the target attribute.

Learn how to convert your Thesis site from XHTML Strict to HTML5!

Tab for a Cause

Obviously, keeping visitors on your site is a positive thing, but all of this talk about tabs reminds me of an amazing way to spread positivity: <a href=”/get-tfac/”>Tab for a Cause</a>!

Tab for a Cause is a browser extension which turns the “new tab” placeholder page into a wonderfully simple tool to raise money for charities, and all you have to do to contribute is keep opening new tabs like you would be anyway!

Do your part to make the world a slightly better place.<a href=”/get-tfac/”>Get Tab for a Cause</a> and keep on pressing that “new tab” button!

17 thoughts on “How to Avoid Losing Visitors Due to Comment Links”

    1. There is a compelling case to made for using WordPress’ popup comments feature; however, for most people (myself included), popup boxes are annoying and may even be blocked by browsers.

      Opening links in new windows at all isn’t an ideal solution, at least not until browsers give users the option to disable such behavior, but it’s far more accepted than popup windows.

  1. Had another question Rick(and this kinda freaked me out in a good way).

    How do you get the “Speak Up, XYZ” in the comment intro? I noticed it on the Timthumb post and was totally blown away!! Adds a whole new spin to the personalisation angle…

    Are you using cookies/custom php? How can one replicate this?

    1. You’re the first person to comment on that! And you’re right, it is a combination of WordPress cookies & custom code. The exact code I’m using follows; simply drop it into custom_functions.php. A proper tutorial on this code will show up sometime in the future, i’m sure.

       * Customizes the text introduction to the list of comments
      function custom_comments_intro( $intro ) {
      	if( isset( $_COOKIE[ 'comment_author_' . COOKIEHASH ] ) )
      		$commenter = ', ' . $_COOKIE[ 'comment_author_' . COOKIEHASH ];
      		$commenter = '';
      	$intro = str_replace( ' comment', ' voice', $intro );
      	$intro = str_replace( '&#8230; read it below or', ' in the conversation.', $intro );
      	$intro = str_replace( '&#8230; read them below or', ' in the conversation.', $intro );
      	$intro = str_replace( 'add one', 'Speak up' . $commenter . '!', $intro );
      	$intro = str_replace( ' trackback', ' linkback', $intro );
      	return $intro;
      add_filter( 'thesis_comments_intro', 'custom_comments_intro' );
    1. A styles question? Hmm… That’s cruel, man. I know how to turn on post formats in Thesis — link, quote, etc. — but styling them well is not something i’ve yet to pull off, which is why i don’t use them here.

                1. There are a couple of things which could be wrong. Make sure you’re using the latest version of WordPress, and make sure the external link box isn’t hidden in “Screen Options” at the top of the post. Also, the box will only appear for single post adding/editing, not for Pages.

                  When it works, the box should appear just above the custom fields meta box, but it can be dragged to any other location.

                  1. Houston, we have liftoff!! I can’t thank you enough for this Rick…it must have taken you ages to bang it out. Works like a charm…I can see the meta box now. Question: would this also hold good for teasers if I knock out the is_single?

                    1. Possibly. Without looking at the code, i’d suspect that you’d need another conditional in there to detect whether the header is for a teaser or not (unsure if the markup for teaser headlines is the same). You could certainly try it; worst case scenario, you just have to add the conditional back in.

Leave a Comment

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

Use your Gravatar-enabled email address while commenting to automatically enhance your comment with some of Gravatar's open profile data.

Comments must be made in accordance with the comment policy. This site uses Akismet to reduce spam; learn how your comment data is processed.

You may use Markdown to format your comments; additionally, these HTML tags and attributes may be used: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.

the Rick Beckman archive
Scroll to Top