Adds a target attribute to all links in passed content.
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.
$contentstringrequired
$targetstringoptional
Default:'_blank'
$tagsstring[]optional
Default:array('a')
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 );
}
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/links_add_target