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');
  • 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…

  • 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.

  • 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?

  • 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

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

  • Saad

    Thanks For sharing really helpful… You have solved my problem I was searching for this for 2 days…

  • Herculis Thakkar

    thanks alot it’s works for new wordpress version