Title: wp_resource_hints
Published: August 16, 2016
Last modified: August 20, 2026

---

# wp_resource_hints()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#wp--skip-link--target)

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

## 󠀁[Description](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#description)󠁿

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"…">`.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#source)󠁿

    ```php
    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";
    		}
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/general-template.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/general-template.php#L3844)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/general-template.php#L3844-L3949)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_resource_hints’, array $urls, string $relation_type )](https://developer.wordpress.org/reference/hooks/wp_resource_hints/)

Filters domains and URLs for resource hints of the given relation type.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_dependencies_unique_hosts()](https://developer.wordpress.org/reference/functions/wp_dependencies_unique_hosts/)`wp-includes/general-template.php` |

Retrieves a list of unique hosts of all enqueued scripts and styles.

  | 
| [wp_parse_url()](https://developer.wordpress.org/reference/functions/wp_parse_url/)`wp-includes/http.php` |

A wrapper for PHP’s parse_url() function that handles consistency in the return values across PHP versions.

  | 
| [esc_url()](https://developer.wordpress.org/reference/functions/esc_url/)`wp-includes/formatting.php` |

Checks and cleans a URL.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.6.0](https://developer.wordpress.org/reference/since/4.6.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#comment-content-4752)
 2.    [rmdundon](https://profiles.wordpress.org/rmdundon/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/wp_resource_hints/#comment-4752)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_resource_hints%2F%23comment-4752)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_resource_hints%2F%23comment-4752)
 4.  Sometimes, you may see this, removing the entire action in order to remove the
     reference to w.org:
 5.      ```php
         remove_action( 'wp_head', 'wp_resource_hints', 2 ); // removes dns-prefetch for w.org
         ```
     
 6.  However, that eliminates the benefit of having dns-prefetch automatically added
     for external scripts.
 7.  An alternative is just to remove the hard-coded reference, and keep dns-prefetch
     for performance gains:
 8.      ```php
         add_filter( 'wp_resource_hints', function( $urls ) {
     
             foreach ($urls as $key => $url) {
                 // Remove dns-prefetch for w.org (we really don't need it)
                 // See https://core.trac.wordpress.org/ticket/40426 for details 
                 if ( 'https://s.w.org/images/core/emoji/13.0.0/svg/' === $url ) {
                     unset( $urls[ $key ] );
                 }
             }
     
             return $urls;
         } );
         ```
     
 9.   * While this example may give a general idea about how to remove something from
        the resource hints, there are often more future-proof ways to do the same thing
        when it comes to removing hints that WordPress has added. For example, the 
        code above could be replaced by adding this to a theme’s functions.php file…`
        add_filter( 'emoji_svg_url', '__return_false', PHP_INT_MAX );`
      * [wp_kc](https://profiles.wordpress.org/wp_kc/) [5 years ago](https://developer.wordpress.org/reference/functions/wp_resource_hints/#comment-5202)
 10.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_resource_hints%2F%3Freplytocom%3D4752%23feedback-editor-4752)
 11.  [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_resource_hints/?output_format=md#comment-content-5419)
 12.   [luboslives](https://profiles.wordpress.org/luboslives/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/wp_resource_hints/#comment-5419)
 13. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_resource_hints%2F%23comment-5419)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_resource_hints%2F%23comment-5419)
 14. The newer way to remove (or alter) the DNS prefetch for s.w.org is to use the 
     [emoji_svg_url](https://developer.wordpress.org/reference/hooks/emoji_svg_url/)
     filter introduced in 4.6.0.
 15.     ```php
         // Remove WP Emoji DNS prefetch from document head
         add_filter( 'emoji_svg_url', '__return_false' );
         ```
     
 16. See [https://core.trac.wordpress.org/attachment/ticket/40426/40426.diff](https://core.trac.wordpress.org/attachment/ticket/40426/40426.diff)
 17.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_resource_hints%2F%3Freplytocom%3D5419%23feedback-editor-5419)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_resource_hints%2F)
before being able to contribute a note or feedback.