wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_targeted_link_rel › WordPress Function

Since5.1.0
Deprecated6.7.0
wp_targeted_link_rel ( $text )
Parameters:
  • (string) $text Content that may contain HTML A elements.
    Required: Yes
Returns:
  • (string) Converted content.
Defined at:
Codex:
Change Log:
  • 5.6.0

Adds `rel="noopener"` to all HTML A elements that have a target.



Source

function wp_targeted_link_rel( $text ) {
	_deprecated_function( __FUNCTION__, '6.7.0' );

	// Don't run (more expensive) regex if no links with targets.
	if ( stripos( $text, 'target' ) === false || stripos( $text, '<a ' ) === false || is_serialized( $text ) ) {
		return $text;
	}

	$script_and_style_regex = '/<(script|style).*?<\/\\1>/si';

	preg_match_all( $script_and_style_regex, $text, $matches );
	$extra_parts = $matches[0];
	$html_parts  = preg_split( $script_and_style_regex, $text );

	foreach ( $html_parts as &$part ) {
		$part = preg_replace_callback( '|<a\s([^>]*target\s*=[^>]*)>|i', 'wp_targeted_link_rel_callback', $part );
	}

	$text = '';
	for ( $i = 0; $i < count( $html_parts ); $i++ ) {
		$text .= $html_parts[ $i ];
		if ( isset( $extra_parts[ $i ] ) ) {
			$text .= $extra_parts[ $i ];
		}
	}

	return $text;
}