• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WordPress core a2z

WordPress core a2z

WordPress core only

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / wp_resource_hints() – Prints resource hints to browsers for pre-fetching, pre-rendering and pre-connecting to web sites.

You appear to be a bot. Output may be restricted

Description

Prints resource hints to browsers for pre-fetching, pre-rendering and pre-connecting to web sites.

Gives hints to browsers to prefetch specific pages or render them in the background, to perform DNS lookups or to begin the connection handshake (DNS, TCP, TLS) in the background. These performance improving indicators work by using `<link rel"…">`.

Usage

wp_resource_hints();

Parameters

Returns

void

Source

File name: wordpress/wp-includes/general-template.php
Lines:

1 to 97 of 97
function wp_resource_hints() {
  $hints = array(
    'dns-prefetch' => wp_dependencies_unique_hosts(),
    'preconnect'   => array(),
    'prefetch'     => array(),
    'prerender'    => array(),
  );

  /*
	 * Add DNS prefetch for the Emoji CDN.
	 * The path is removed in the foreach loop below.
	 */
  
/** This filter is documented in wp-includes/formatting.php */
  $hints['dns-prefetch'][] = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/13.0.0/svg/' );

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

    
/**
 * Filters domains and URLs for resource hints of relation type.
 *
 * @since 4.6.0
 *
 * @param array  $urls          URLs to print for resource hints.
 * @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";
    }
  }
}
 

 View on GitHub View on Trac

Published: 25th November 2019 | Last updated: 21st August 2020

Primary Sidebar

Information

Function name: wp_resource_hints
Plugin ref: WordPress
Version: 5.6.2
Sourcefile: wp-includes/general-template.php
File ref: wp-includes/general-template.php
Deprecated?: No
API Letters: H,R,W

Footer

WP-a2z
WordPress core a2z
WordPress core only
WordPress 5.6.2
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  core.wp-a2z.org
© Copyright WP-a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites