Snippets

Add target=”_blank” on all link

just add in function.php

function autoblank($text) {
	$return = str_replace('<a', '<a target="_blank"', $text);
	return $return;
}
add_filter('the_content', 'autoblank');

Share this snippet

 
  • http://demeyere.com/ Ross DeMeyere

    Personally, I use jQuery to open external links (only) in a new browser window. The problem with the method shown is that any links to the same site will also open in a new window…

  • http://www.simonwhatley.co.uk Simon Whatley

    This code snippet doesn’t descriminate between links that have had the target=”_blank” already applied by the user and those that have not. It would be better to first do a regex search of the anchor to see what attributes have been applied, then apply the appropriate ones.

    As Ross also suggests, there needs to be a way of distinguishing between internal and external links before the target attribute is applied. You could use the rel or class attribute for this, but it still requires some form of manual input.

  • http://fmsiam.org/index.php?action=profile;u=100303 Ravi Deshmukh

    Hmm is anyone else encountering problems with the images on this blog loading?
    I’m trying to figure out if its a problem on my end or if it’s the blog.
    Any feed-back would be greatly appreciated.

    • Andrey Beketov

      What problems?

  • http://demeyere.com/ Ross DeMeyere

    Following up on this thread, I am using jQuery to target all links that are not in my domain and adding target _blank. I want all links leaving my site opening in a new window… (this goes in the `$(document).ready(function()`)

    // open all external links in a new window
    // 2012-11-29 20:34
    $(“a[href^='http']“).not(“[href*='demeyere.com']“).attr(‘target’,’_blank’);

  • http://demeyere.com/ Ross DeMeyere

    previous comment is cut off because the comment doesn’t wrap… look at the source code.