wp_targeted_link_rel

函数
wp_targeted_link_rel ( $text )
参数
  • (string) $text Content that may contain HTML A elements.
    Required:
返回值
  • (string) Converted content.
定义位置
相关方法
wp_targeted_link_rel_callbackwp_init_targeted_link_rel_filterswp_remove_targeted_link_rel_filterswp_get_linkswp_make_link_relative
引入
5.1.0
弃用
-

wp_targeted_link_rel 是一个为外部链接添加”rel”属性的函数,以便识别它们,并为这些链接应用特定的样式,例如,将它们与内部链接区分开来。

为所有有目标的HTML A元素添加`rel="noopener"`。

function wp_targeted_link_rel( $text ) {
	// 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( '|<as([^>]*targets*=[^>]*)>|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;
}

常见问题

FAQs
查看更多 >