links_add_target()WP 2.7.0

Adds a Target attribute to all links in passed content.

This function by default only applies to <a> tags, however this can be modified by the 3rd param.

NOTE: Any current target attributed will be stripped and replaced.

1 time — 0.000099 sec (very fast) | 50000 times — 0.38 sec (very fast) | PHP 7.0.8, WP 4.6

No Hooks.


String. The processed content.


links_add_target( $content, $target, $tags );
$content(string) (required)
String to search for links in.
The Target to add to the links.
Default: '_blank'
An array of tags to apply to.
Default: array( fooo )


  • Global. String. $_links_add_target


Since 2.7.0 Introduced.

Code of links_add_target() WP 6.0

function links_add_target( $content, $target = '_blank', $tags = array( 'a' ) ) {
	global $_links_add_target;
	$_links_add_target = $target;
	$tags              = implode( '|', (array) $tags );
	return preg_replace_callback( "!<($tags)((\s[^>]*)?)>!i", '_links_add_target', $content );