Title: wp_targeted_link_rel_callback
Published: February 22, 2019
Last modified: August 20, 2026

---

# wp_targeted_link_rel_callback( array $matches ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#wp--skip-link--target)

This function has been deprecated since 6.7.0.

Callback to add `rel="noopener"` string to HTML A element.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#description)󠁿

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#parameters)󠁿

 `$matches`arrayrequired

Single match.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#return)󠁿

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

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#source)󠁿

    ```php
    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>";
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/formatting.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/formatting.php#L3428)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/formatting.php#L3428-L3471)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_targeted_link_rel’, string $rel, string $link_html )](https://developer.wordpress.org/reference/hooks/wp_targeted_link_rel/)

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_kses_hair()](https://developer.wordpress.org/reference/functions/wp_kses_hair/)`wp-includes/kses.php` |

Given a string of HTML attributes and values, parse into a structured attribute list.

  | 
| [wp_allowed_protocols()](https://developer.wordpress.org/reference/functions/wp_allowed_protocols/)`wp-includes/functions.php` |

Retrieves a list of protocols to allow in HTML attributes.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [_deprecated_function()](https://developer.wordpress.org/reference/functions/_deprecated_function/)`wp-includes/functions.php` |

Marks a function as deprecated and inform when it has been used.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_targeted_link_rel_callback/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.7.0](https://developer.wordpress.org/reference/since/6.7.0/) | Deprecated.  | 
| [5.6.0](https://developer.wordpress.org/reference/since/5.6.0/) | Removed `'noreferrer'` relationship. | 
| [5.1.0](https://developer.wordpress.org/reference/since/5.1.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_targeted_link_rel_callback%2F)
before being able to contribute a note or feedback.