wp_resource_hints

The timeline below displays how wordpress function wp_resource_hints has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.5

/**
 * Prints resource hints to browsers for pre-fetching, pre-rendering
 * and pre-connecting to websites.
 *
 * 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"…">`.
 *
 * @since 4.6.0
 */
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 the given 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. One of
         *                              'dns-prefetch', 'preconnect', 'prefetch', 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";
        }
    }
}

WordPress Version: 6.1

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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";
        }
    }
}

WordPress Version: 5.8

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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
         * @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";
        }
    }
}

WordPress Version: 5.5

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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";
        }
    }
}

WordPress Version: 5.2

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/12.0.0-1/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'))) {
                $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";
        }
    }
}

WordPress Version: 5.1

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/11.2.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'))) {
                $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";
        }
    }
}

WordPress Version: 9.8

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/11/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'))) {
                $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";
        }
    }
}

WordPress Version: 9.3

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/2.4/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'))) {
                $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";
        }
    }
}

WordPress Version: .20

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/11/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'))) {
                $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";
        }
    }
}

WordPress Version: 9.2

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/2.3/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'))) {
                $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";
        }
    }
}

WordPress Version: .10

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/11/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'))) {
                $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";
        }
    }
}

WordPress Version: 4.8

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/2.3/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'))) {
                $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";
        }
    }
}

WordPress Version: 4.7

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/2.2.1/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'))) {
                $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";
        }
    }
}

WordPress Version: 6.1

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/2/svg/');
    foreach ($hints as $relation_type => $urls) {
        /**
         * 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) {
            $url = esc_url($url, array('http', 'https'));
            if (!$url) {
                unset($urls[$key]);
                continue;
            }
            if (in_array($relation_type, array('preconnect', 'dns-prefetch'))) {
                $parsed = wp_parse_url($url);
                if (empty($parsed['host'])) {
                    unset($urls[$key]);
                    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'];
                }
            }
            $urls[$key] = $url;
        }
        $urls = array_unique($urls);
        foreach ($urls as $url) {
            printf("<link rel='%s' href='%s' />\n", $relation_type, $url);
        }
    }
}

WordPress Version: 4.6

/**
 * 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"…">`.
 *
 * @since 4.6.0
 */
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/2/svg/');
    foreach ($hints as $relation_type => $urls) {
        /**
         * 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) {
            $url = esc_url($url, array('http', 'https'));
            if (!$url) {
                unset($urls[$key]);
                continue;
            }
            if (in_array($relation_type, array('preconnect', 'dns-prefetch'))) {
                $parsed = wp_parse_url($url);
                if (empty($parsed['host'])) {
                    unset($urls[$key]);
                    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'];
                }
            }
            $urls[$key] = $url;
        }
        $urls = array_unique($urls);
        foreach ($urls as $url) {
            printf("<link rel='%s' href='%s'>\n", $relation_type, $url);
        }
    }
}