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


Comments (6) - Leave yours

  1. Ross DeMeyere said:

    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…

  2. Simon Whatley said:

    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.

  3. Ravi Deshmukh said:

    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.

  4. Ross DeMeyere said:

    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

Leave a Reply

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

Please respect the following rules: No advertising, no spam, no keyword in name field. Thank you!