links_add_target( string $content, string $target = ‘_blank’, string[] $tags = array(‘a’) ): string

Adds a target attribute to all links in passed content.

Description

By default, this function only applies to <a> tags.
However, this can be modified via the $tags parameter.

_NOTE:_ Any current target attribute will be stripped and replaced.

Parameters

$contentstringrequired
String to search for links in.
$targetstringoptional
The target to add to the links.

Default:'_blank'

$tagsstring[]optional
An array of tags to apply to.

Default:array('a')

Return

string The processed content.

Source

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 );
}

Changelog

VersionDescription
2.7.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.