Outil de recherche WordPress pour les développeurs et auteurs de thèmes



wp_rel_nofollow_callback ›

Depuis2.3.0
Dépréciéen/a
wp_rel_nofollow_callback ( $matches )
Paramètres:
  • (array) $matches Single Match
    Requis: Oui
Retourne:
  • (string) HTML A Element with rel nofollow.
Défini(e) dans:
Codex:

Callback to add rel=nofollow string to HTML A element.

Will remove already existing rel="nofollow" and rel='nofollow' from the string to prevent from invalidating (X)HTML.



Source

function wp_rel_nofollow_callback( $matches ) {
	$text = $matches[1];
	$atts = wp_kses_hair( $matches[1], wp_allowed_protocols() );
	$rel  = 'nofollow';

	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'] ) );
		if ( false === array_search( 'nofollow', $parts ) ) {
			$parts[] = 'nofollow';
		}
		$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 ) . '">';
}