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. Avinash D'Souza says:
    35x finance supergeek, 5x eco buff, 40x commonsenser, 20x enabler=100x authentic. Sum ergo cogito. But ego operor>ego cogito. So…what I can do for you today?

    Maybe it’s just me, but I’d rather have the comments load in a pop-up box…that way as soon as they’re done with the comments, they’re back on my site.

    • Rick Beckman says:
      Student of the sciences, the religions, the science fictions, and the fantasies… But mostly I’m just trying to find my groove in this big, crazy world.

      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.

  2. Avinash D'Souza says:
    35x finance supergeek, 5x eco buff, 40x commonsenser, 20x enabler=100x authentic. Sum ergo cogito. But ego operor>ego cogito. So…what I can do for you today?

    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?

    • Rick Beckman says:
      Student of the sciences, the religions, the science fictions, and the fantasies… But mostly I’m just trying to find my groove in this big, crazy world.

      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 ];
      	else
      		$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' );
      • Avinash D'Souza says:
        35x finance supergeek, 5x eco buff, 40x commonsenser, 20x enabler=100x authentic. Sum ergo cogito. But ego operor>ego cogito. So…what I can do for you today?

        Haven’t seen this on any other site. Period. Seen some gorgeous CSS work but the PHP is pretty vanilla.

        Stuff like this blows me away…

        Love your daily/weekly Thesis snippets…

        • Rick Beckman says:
          Student of the sciences, the religions, the science fictions, and the fantasies… But mostly I’m just trying to find my groove in this big, crazy world.

          Style isn’t my strong suit at all. Code tweaks, however… Well, I can rock those. Keep your eyes on my WordPress directory for all my latest tutorials. Not much there yet, but i’m going to keep working on it! (It’s all targeted to Thesis users, of course.)

  3. Avinash D'Souza says:
    35x finance supergeek, 5x eco buff, 40x commonsenser, 20x enabler=100x authentic. Sum ergo cogito. But ego operor>ego cogito. So…what I can do for you today?

    If you ever have the time for it, would love to see a post on post formats styling…

    • Rick Beckman says:
      Student of the sciences, the religions, the science fictions, and the fantasies… But mostly I’m just trying to find my groove in this big, crazy world.

      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.

        • Rick Beckman says:
          Student of the sciences, the religions, the science fictions, and the fantasies… But mostly I’m just trying to find my groove in this big, crazy world.

          Workin’ on it for ya. Keep your eye on the forum thread!

          • Avinash D'Souza says:
            35x finance supergeek, 5x eco buff, 40x commonsenser, 20x enabler=100x authentic. Sum ergo cogito. But ego operor>ego cogito. So…what I can do for you today?

            Saw the forum post….thanks a ton for that! A partial solution is better than no solution at all…

            • Rick Beckman says:
              Student of the sciences, the religions, the science fictions, and the fantasies… But mostly I’m just trying to find my groove in this big, crazy world.

              Check the thread again. ;)

              • Avinash D'Souza says:
                35x finance supergeek, 5x eco buff, 40x commonsenser, 20x enabler=100x authentic. Sum ergo cogito. But ego operor>ego cogito. So…what I can do for you today?

                Almost there!! The code is working fine cos no parse errors….but I don’t see any meta box! :-(

                • Rick Beckman says:
                  Student of the sciences, the religions, the science fictions, and the fantasies… But mostly I’m just trying to find my groove in this big, crazy world.

                  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.

                  • Avinash D'Souza says:
                    35x finance supergeek, 5x eco buff, 40x commonsenser, 20x enabler=100x authentic. Sum ergo cogito. But ego operor>ego cogito. So…what I can do for you today?

                    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?

                    • Rick Beckman says:
                      Student of the sciences, the religions, the science fictions, and the fantasies… But mostly I’m just trying to find my groove in this big, crazy world.

                      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.

                    • Avinash D'Souza says:
                      35x finance supergeek, 5x eco buff, 40x commonsenser, 20x enabler=100x authentic. Sum ergo cogito. But ego operor>ego cogito. So…what I can do for you today?

                      A little more help over at the forums? Coming a cropper…

Join the Discussion

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

Comments must be made in accordance with the comment policy. Use your Gravatar-enabled email address while commenting to automatically enhance your comment with some of Gravatar's open profile data.

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>