bbp_rel_nofollow_callback( array $matches = array() )

Adds rel=nofollow to a link


Description Description


Parameters Parameters

$matches

(Optional)

Default value: array()


Top ↑

Return Return

(string) $text Link with rel=nofollow added


Top ↑

Source Source

File: includes/common/formatting.php

function bbp_rel_nofollow_callback( $matches = array() ) {
	$text     = $matches[1];
	$atts     = shortcode_parse_atts( $matches[1] );
	$rel      = 'nofollow';
	$home_url = home_url();

	// Bail on links that match the current domain
	if ( preg_match( '%href=["\'](' . preg_quote( set_url_scheme( $home_url, 'http'  ) ) . ')%i', $text ) ||
	     preg_match( '%href=["\'](' . preg_quote( set_url_scheme( $home_url, 'https' ) ) . ')%i', $text )
	) {
		return "<a {$text}>";
	}

	// Avoid collisions with existing "rel" attribute
	if ( ! empty( $atts['rel'] ) ) {
		$parts = array_map( 'trim', explode( ' ', $atts['rel'] ) );
		if ( false === array_search( 'nofollow', $parts ) ) {
			$parts[] = 'nofollow';
		}

		$rel = implode( ' ', $parts );
		unset( $atts['rel'] );

		$html = '';
		foreach ( $atts as $name => $value ) {
			$html .= "{$name}=\"{$value}\" ";
		}

		$text = trim( $html );
	}

	return "<a {$text} rel=\"{$rel}\">";
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.