_make_clickable_rel_attr

函式
_make_clickable_rel_attr ( $url )
引數
  • (string) $url The URL.
    Required:
返回值
  • (string) The rel attribute for the anchor or an empty string if no rel attribute should be added.
定義位置
相關方法
make_clickable_make_url_clickable_cb_make_email_clickable_cbwp_make_link_relative_make_web_ftp_clickable_cb
引入
6.2.0
棄用
-

_make_clickable_rel_attr(): 這個函式用來給在一串文字中發現的任何連結新增一個rel屬性。這對於為連結新增rel屬性以防止搜尋引擎機器人跟蹤它們,或指定連結應在新視窗或標籤頁中開啟是很有用的。

function _make_clickable_rel_attr( $url ) {
	$rel_parts        = array();
	$scheme           = strtolower( wp_parse_url( $url, PHP_URL_SCHEME ) );
	$nofollow_schemes = array_intersect( wp_allowed_protocols(), array( 'https', 'http' ) );

	// Apply "nofollow" to external links with qualifying URL schemes (mailto:, tel:, etc... shouldn't be followed).
	if ( ! wp_is_internal_link( $url ) && in_array( $scheme, $nofollow_schemes, true ) ) {
		$rel_parts[] = 'nofollow';
	}

	// Apply "ugc" when in comment context.
	if ( 'comment_text' === current_filter() ) {
		$rel_parts[] = 'ugc';
	}

	$rel = implode( ' ', $rel_parts );

	/**
	 * Filters the rel value that is added to URL matches converted to links.
	 *
	 * @since 5.3.0
	 *
	 * @param string $rel The rel value.
	 * @param string $url The matched URL being converted to a link tag.
	 */
	$rel = apply_filters( 'make_clickable_rel', $rel, $url );

	$rel_attr = $rel ? ' rel="' . esc_attr( $rel ) . '"' : '';

	return $rel_attr;
}

常見問題

FAQs
檢視更多 >