wp_rel_callback

函式
wp_rel_callback ( $matches, $rel )
引數
  • (array) $matches Single match.
    Required:
  • (string) $rel The rel attribute to add.
    Required:
返回值
  • (string) HTML A element with the added rel attribute.
定義位置
相關方法
wp_rel_nofollow_callback_sort_name_callback_sort_uname_callbackwp_check_jsonp_callbackwp_targeted_link_rel_callback
引入
5.3.0
棄用
-

wp_rel_callback: 這個函式用來給連結新增rel屬性。它接受幾個引數,包括連結的屬性和要新增的rel屬性的型別。

回撥,向HTML A元素新增rel屬性。

在新增前會刪除已經存在的字串,以防止無效的(X)HTML。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_rel_callback( $matches, $rel ) {
$text = $matches[1];
$atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );
if ( ! empty( $atts['href'] ) ) {
if ( in_array( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) {
if ( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) {
return "<a $text>";
}
}
}
if ( ! empty( $atts['rel'] ) ) {
$parts = array_map( 'trim', explode( ' ', $atts['rel']['value'] ) );
$rel_array = array_map( 'trim', explode( ' ', $rel ) );
$parts = array_unique( array_merge( $parts, $rel_array ) );
$rel = implode( ' ', $parts );
unset( $atts['rel'] );
$html = '';
foreach ( $atts as $name => $value ) {
if ( isset( $value['vless'] ) && 'y' === $value['vless'] ) {
$html .= $name . ' ';
} else {
$html .= "{$name}="" . esc_attr( $value['value'] ) . '" ';
}
}
$text = trim( $html );
}
return "<a $text rel="" . esc_attr( $rel ) . '">';
}
function wp_rel_callback( $matches, $rel ) { $text = $matches[1]; $atts = wp_kses_hair( $matches[1], wp_allowed_protocols() ); if ( ! empty( $atts['href'] ) ) { if ( in_array( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) { if ( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) { return "<a $text>"; } } } if ( ! empty( $atts['rel'] ) ) { $parts = array_map( 'trim', explode( ' ', $atts['rel']['value'] ) ); $rel_array = array_map( 'trim', explode( ' ', $rel ) ); $parts = array_unique( array_merge( $parts, $rel_array ) ); $rel = implode( ' ', $parts ); unset( $atts['rel'] ); $html = ''; foreach ( $atts as $name => $value ) { if ( isset( $value['vless'] ) && 'y' === $value['vless'] ) { $html .= $name . ' '; } else { $html .= "{$name}="" . esc_attr( $value['value'] ) . '" '; } } $text = trim( $html ); } return "<a $text rel="" . esc_attr( $rel ) . '">'; }
function wp_rel_callback( $matches, $rel ) {
	$text = $matches[1];
	$atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );

	if ( ! empty( $atts['href'] ) ) {
		if ( in_array( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_SCHEME ) ), array( 'http', 'https' ), true ) ) {
			if ( strtolower( wp_parse_url( $atts['href']['value'], PHP_URL_HOST ) ) === strtolower( wp_parse_url( home_url(), PHP_URL_HOST ) ) ) {
				return "<a $text>";
			}
		}
	}

	if ( ! empty( $atts['rel'] ) ) {
		$parts     = array_map( 'trim', explode( ' ', $atts['rel']['value'] ) );
		$rel_array = array_map( 'trim', explode( ' ', $rel ) );
		$parts     = array_unique( array_merge( $parts, $rel_array ) );
		$rel       = implode( ' ', $parts );
		unset( $atts['rel'] );

		$html = '';
		foreach ( $atts as $name => $value ) {
			if ( isset( $value['vless'] ) && 'y' === $value['vless'] ) {
				$html .= $name . ' ';
			} else {
				$html .= "{$name}="" . esc_attr( $value['value'] ) . '" ';
			}
		}
		$text = trim( $html );
	}
	return "<a $text rel="" . esc_attr( $rel ) . '">';
}

常見問題

FAQs
檢視更多 >