函数文档

wp_targeted_link_rel_callback()

💡 云策文档标注

概述

wp_targeted_link_rel_callback() 是一个用于向 HTML A 元素添加 rel="noopener" 属性的回调函数,自 WordPress 6.7.0 起已被弃用。该函数通过正则表达式匹配处理链接属性,避免重复添加现有值以保持 HTML 有效性。

关键要点

  • 函数 wp_targeted_link_rel_callback() 已弃用,自 6.7.0 版本起不建议使用。
  • 核心功能是向带有 target 属性的链接添加 rel="noopener",并过滤重复值。
  • 使用 wp_kses_hair() 解析 HTML 属性,支持转义字符处理。
  • 通过 apply_filters('wp_targeted_link_rel', ...) 钩子允许自定义 rel 值。
  • 函数返回修改后的 HTML A 元素字符串,若无 target 属性则返回空字符串。

代码示例

function wp_targeted_link_rel_callback( $matches ) {
    _deprecated_function( __FUNCTION__, '6.7.0' );
    $link_html = $matches[1];
    // 处理转义和属性解析
    $atts = wp_kses_hair( $link_html, wp_allowed_protocols() );
    $rel = apply_filters( 'wp_targeted_link_rel', 'noopener', $link_html );
    if ( ! $rel || ! isset( $atts['target'] ) ) {
        return "";
    }
    // 合并和去重 rel 值
    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' ) );
    return $link_html;
}

注意事项

  • 该函数已弃用,开发者应避免在新代码中使用,并考虑替代方案。
  • 函数处理转义字符以确保 HTML 解析正确,需注意正则表达式匹配逻辑。
  • 通过 wp_targeted_link_rel 过滤器可以修改添加的 rel 值,增强灵活性。
  • 函数仅在链接有 target 属性时添加 rel,否则返回空字符串。

📄 原文内容

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

Description

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

Parameters

$matchesarrayrequired
Single match.

Return

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

Source

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

Hooks

apply_filters( ‘wp_targeted_link_rel’, string $rel, string $link_html )

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

Changelog

Version Description
6.7.0 Deprecated.
5.6.0 Removed 'noreferrer' relationship.
5.1.0 Introduced.