wppaste
WordPress

wp_targeted_link_rel_callback( array $matches ): string

Since
5.1.0, 5.6.0
Source
wp-includes/formatting.php:3428
Callback to add rel="noopener" string to HTML A element.

Description

Will not duplicate an existing 'noopener' value to avoid invalidating the HTML.

Compatibility

Deprecated in WordPress 6.7.0. Kept for back-compatibility; avoid it in new code.

WordPress
since 5.6.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$matchesarray
Single match.

Return value

string
HTML A Element with rel="noopener" in addition to any existing values.

Performance profile

How much work a call to wp_targeted_link_rel_callback() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
28–70

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 74.

Plugin surface
1 hook

Third-party callbacks on 'wp_targeted_link_rel' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • regexregular expression over the whole inputpreg_split()called directly

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 3 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_targeted_link_rel_callback() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always28–33_deprecated_function(), wp_allowed_protocols(), wp_kses_hair(), apply_filters()
always47–53_deprecated_function(), wp_allowed_protocols(), wp_kses_hair(), apply_filters(), esc_attr(), array_column()
isset($atts)64–70_deprecated_function(), wp_allowed_protocols(), wp_kses_hair(), apply_filters(), preg_split(), array_unique(), esc_attr(), array_column()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7428–705
8.57428–705
8.47428–70515 fewer instructions than PHP 8.3
8.38931–855
8.28931–855
8.18931–855
7.48931–855

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while wp_targeted_link_rel_callback() runs, in this order:

  1. apply_filters( wp_targeted_link_rel )filterline 3451 (+23 into the body)

    Filters the rel values that are added to links with `target` attribute.

Uses · 5

Source code

function wp_targeted_link_rel_callback( $matches ) {	_deprecated_function( __FUNCTION__, '6.7.0' ); 	$link_html          = $matches[1];	$original_link_html = $link_html; 	// Consider the HTML escaped if there are no unescaped quotes.	$is_escaped = ! preg_match( '/(^|[^\\\\])[\'"]/', $link_html );	if ( $is_escaped ) {		// Replace only the quotes so that they are parsable by wp_kses_hair(), leave the rest as is.		$link_html = preg_replace( '/\\\\([\'"])/', '$1', $link_html );	} 	$atts = wp_kses_hair( $link_html, wp_allowed_protocols() ); 	/**	 * Filters the rel values that are added to links with `target` attribute.	 *	 * @since 5.1.0	 *	 * @param string $rel       The rel values.	 * @param string $link_html The matched content of the link tag including all HTML attributes.	 */	$rel = apply_filters( 'wp_targeted_link_rel', 'noopener', $link_html ); 	// Return early if no rel values to be added or if no actual target attribute.	if ( ! $rel || ! isset( $atts['target'] ) ) {		return "<a $original_link_html>";	} 	if ( isset( $atts['rel'] ) ) {		$all_parts = preg_split( '/\s/', "{$atts['rel']['value']} $rel", -1, PREG_SPLIT_NO_EMPTY );		$rel       = implode( ' ', array_unique( $all_parts ) );	} 	$atts['rel']['whole'] = 'rel="' . esc_attr( $rel ) . '"';	$link_html            = implode( ' ', array_column( $atts, 'whole' ) ); 	if ( $is_escaped ) {		$link_html = preg_replace( '/[\'"]/', '\\\\$0', $link_html );	} 	return "<a $link_html>";}

Changelog

Introduced in 5.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.7.0
Deprecated.from the docblock
5.6.0
Removed 'noreferrer' relationship.from the docblock
5.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/formatting.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.