links_add_target() WordPress Function

The links_add_target() function allows you to add a target attribute to all links on your website. This is useful if you want to open all links in a new window or tab.

links_add_target( string $content, string $target = '_blank', string[] $tags = array('a') ) #

Adds a Target attribute to all links in passed content.


Description

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.


Top ↑

Parameters

$content

(string)(Required)String to search for links in.

$target

(string)(Optional)The Target to add to the links.

Default value: '_blank'

$tags

(string[])(Optional)An array of tags to apply to.

Default value: array('a')


Top ↑

Return

(string) The processed content.


Top ↑

Source

File: wp-includes/formatting.php

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


Top ↑

Changelog

Changelog
VersionDescription
2.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More