wp_resource_hints

函式
wp_resource_hints ( No parameters )

wp_resource_hints: 這個函式向WordPress網站新增資源提示,指出哪些資源應該被預載入或預取。這可以通過減少載入資源的時間來提高網站的效能。

向瀏覽器列印資源提示,以便預先獲取、預先渲染和預先連線到網站。

向瀏覽器提供提示,以預取特定頁面或在後臺渲染,執行DNS查詢或在後臺開始連線握手(DNS、TCP、TLS)。

這些效能改進指標通過使用“。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function wp_resource_hints() {
$hints = array(
'dns-prefetch' => wp_dependencies_unique_hosts(),
'preconnect' => array(),
'prefetch' => array(),
'prerender' => array(),
);
foreach ( $hints as $relation_type => $urls ) {
$unique_urls = array();
/**
* Filters domains and URLs for resource hints of relation type.
*
* @since 4.6.0
* @since 4.7.0 The `$urls` parameter accepts arrays of specific HTML attributes
* as its child elements.
*
* @param array $urls {
* Array of resources and their attributes, or URLs to print for resource hints.
*
* @type array|string ...$0 {
* Array of resource attributes, or a URL string.
*
* @type string $href URL to include in resource hints. Required.
* @type string $as How the browser should treat the resource
* (`script`, `style`, `image`, `document`, etc).
* @type string $crossorigin Indicates the CORS policy of the specified resource.
* @type float $pr Expected probability that the resource hint will be used.
* @type string $type Type of the resource (`text/html`, `text/css`, etc).
* }
* }
* @param string $relation_type The relation type the URLs are printed for,
* e.g. 'preconnect' or 'prerender'.
*/
$urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );
foreach ( $urls as $key => $url ) {
$atts = array();
if ( is_array( $url ) ) {
if ( isset( $url['href'] ) ) {
$atts = $url;
$url = $url['href'];
} else {
continue;
}
}
$url = esc_url( $url, array( 'http', 'https' ) );
if ( ! $url ) {
continue;
}
if ( isset( $unique_urls[ $url ] ) ) {
continue;
}
if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ), true ) ) {
$parsed = wp_parse_url( $url );
if ( empty( $parsed['host'] ) ) {
continue;
}
if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) {
$url = $parsed['scheme'] . '://' . $parsed['host'];
} else {
// Use protocol-relative URLs for dns-prefetch or if scheme is missing.
$url = '//' . $parsed['host'];
}
}
$atts['rel'] = $relation_type;
$atts['href'] = $url;
$unique_urls[ $url ] = $atts;
}
foreach ( $unique_urls as $atts ) {
$html = '';
foreach ( $atts as $attr => $value ) {
if ( ! is_scalar( $value )
|| ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ) )
) {
continue;
}
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
if ( ! is_string( $attr ) ) {
$html .= " $value";
} else {
$html .= " $attr='$value'";
}
}
$html = trim( $html );
echo "<link $html />n";
}
}
}
function wp_resource_hints() { $hints = array( 'dns-prefetch' => wp_dependencies_unique_hosts(), 'preconnect' => array(), 'prefetch' => array(), 'prerender' => array(), ); foreach ( $hints as $relation_type => $urls ) { $unique_urls = array(); /** * Filters domains and URLs for resource hints of relation type. * * @since 4.6.0 * @since 4.7.0 The `$urls` parameter accepts arrays of specific HTML attributes * as its child elements. * * @param array $urls { * Array of resources and their attributes, or URLs to print for resource hints. * * @type array|string ...$0 { * Array of resource attributes, or a URL string. * * @type string $href URL to include in resource hints. Required. * @type string $as How the browser should treat the resource * (`script`, `style`, `image`, `document`, etc). * @type string $crossorigin Indicates the CORS policy of the specified resource. * @type float $pr Expected probability that the resource hint will be used. * @type string $type Type of the resource (`text/html`, `text/css`, etc). * } * } * @param string $relation_type The relation type the URLs are printed for, * e.g. 'preconnect' or 'prerender'. */ $urls = apply_filters( 'wp_resource_hints', $urls, $relation_type ); foreach ( $urls as $key => $url ) { $atts = array(); if ( is_array( $url ) ) { if ( isset( $url['href'] ) ) { $atts = $url; $url = $url['href']; } else { continue; } } $url = esc_url( $url, array( 'http', 'https' ) ); if ( ! $url ) { continue; } if ( isset( $unique_urls[ $url ] ) ) { continue; } if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ), true ) ) { $parsed = wp_parse_url( $url ); if ( empty( $parsed['host'] ) ) { continue; } if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) { $url = $parsed['scheme'] . '://' . $parsed['host']; } else { // Use protocol-relative URLs for dns-prefetch or if scheme is missing. $url = '//' . $parsed['host']; } } $atts['rel'] = $relation_type; $atts['href'] = $url; $unique_urls[ $url ] = $atts; } foreach ( $unique_urls as $atts ) { $html = ''; foreach ( $atts as $attr => $value ) { if ( ! is_scalar( $value ) || ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ) ) ) { continue; } $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); if ( ! is_string( $attr ) ) { $html .= " $value"; } else { $html .= " $attr='$value'"; } } $html = trim( $html ); echo "<link $html />n"; } } }
function wp_resource_hints() {
	$hints = array(
		'dns-prefetch' => wp_dependencies_unique_hosts(),
		'preconnect'   => array(),
		'prefetch'     => array(),
		'prerender'    => array(),
	);

	foreach ( $hints as $relation_type => $urls ) {
		$unique_urls = array();

		/**
		 * Filters domains and URLs for resource hints of relation type.
		 *
		 * @since 4.6.0
		 * @since 4.7.0 The `$urls` parameter accepts arrays of specific HTML attributes
		 *              as its child elements.
		 *
		 * @param array  $urls {
		 *     Array of resources and their attributes, or URLs to print for resource hints.
		 *
		 *     @type array|string ...$0 {
		 *         Array of resource attributes, or a URL string.
		 *
		 *         @type string $href        URL to include in resource hints. Required.
		 *         @type string $as          How the browser should treat the resource
		 *                                   (`script`, `style`, `image`, `document`, etc).
		 *         @type string $crossorigin Indicates the CORS policy of the specified resource.
		 *         @type float  $pr          Expected probability that the resource hint will be used.
		 *         @type string $type        Type of the resource (`text/html`, `text/css`, etc).
		 *     }
		 * }
		 * @param string $relation_type The relation type the URLs are printed for,
		 *                              e.g. 'preconnect' or 'prerender'.
		 */
		$urls = apply_filters( 'wp_resource_hints', $urls, $relation_type );

		foreach ( $urls as $key => $url ) {
			$atts = array();

			if ( is_array( $url ) ) {
				if ( isset( $url['href'] ) ) {
					$atts = $url;
					$url  = $url['href'];
				} else {
					continue;
				}
			}

			$url = esc_url( $url, array( 'http', 'https' ) );

			if ( ! $url ) {
				continue;
			}

			if ( isset( $unique_urls[ $url ] ) ) {
				continue;
			}

			if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ), true ) ) {
				$parsed = wp_parse_url( $url );

				if ( empty( $parsed['host'] ) ) {
					continue;
				}

				if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) {
					$url = $parsed['scheme'] . '://' . $parsed['host'];
				} else {
					// Use protocol-relative URLs for dns-prefetch or if scheme is missing.
					$url = '//' . $parsed['host'];
				}
			}

			$atts['rel']  = $relation_type;
			$atts['href'] = $url;

			$unique_urls[ $url ] = $atts;
		}

		foreach ( $unique_urls as $atts ) {
			$html = '';

			foreach ( $atts as $attr => $value ) {
				if ( ! is_scalar( $value )
					|| ( ! in_array( $attr, array( 'as', 'crossorigin', 'href', 'pr', 'rel', 'type' ), true ) && ! is_numeric( $attr ) )
				) {

					continue;
				}

				$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );

				if ( ! is_string( $attr ) ) {
					$html .= " $value";
				} else {
					$html .= " $attr='$value'";
				}
			}

			$html = trim( $html );

			echo "<link $html />n";
		}
	}
}

常見問題

FAQs
檢視更多 >