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!
Leave a Reply to Avinash D'Souza Cancel reply