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

 

Comments (2) - 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.

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!