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"`。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
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;
}
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; }
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
檢視更多 >