Title: wp_preload_resources
Published: November 2, 2022
Last modified: February 24, 2026

---

# wp_preload_resources()

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/wp_preload_resources/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/functions/wp_preload_resources/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_preload_resources/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_preload_resources/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_preload_resources/?output_format=md#changelog)

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

Prints resource preloads directives to browsers.

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

Gives directive to browsers to preload specific resources that website will need
very soon, this ensures that they are available earlier and are less likely to block
the page’s render. Preload directives should not be used for non-render-blocking
elements, as then they would compete with the render-blocking ones, slowing down
the render.

These performance improving indicators work by using `<link rel="preload">`.

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

    ```php
    function wp_preload_resources() {
    	/**
    	 * Filters domains and URLs for resource preloads.
    	 *
    	 * @since 6.1.0
    	 * @since 6.6.0 Added the `$fetchpriority` attribute.
    	 *
    	 * @param array  $preload_resources {
    	 *     Array of resources and their attributes, or URLs to print for resource preloads.
    	 *
    	 *     @type array ...$0 {
    	 *         Array of resource attributes.
    	 *
    	 *         @type string $href          URL to include in resource preloads. 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 string $type          Type of the resource (`text/html`, `text/css`, etc).
    	 *         @type string $media         Accepts media types or media queries. Allows responsive preloading.
    	 *         @type string $imagesizes    Responsive source size to the source Set.
    	 *         @type string $imagesrcset   Responsive image sources to the source set.
    	 *         @type string $fetchpriority Fetchpriority value for the resource.
    	 *     }
    	 * }
    	 */
    	$preload_resources = apply_filters( 'wp_preload_resources', array() );

    	if ( ! is_array( $preload_resources ) ) {
    		return;
    	}

    	$unique_resources = array();

    	// Parse the complete resource list and extract unique resources.
    	foreach ( $preload_resources as $resource ) {
    		if ( ! is_array( $resource ) ) {
    			continue;
    		}

    		$attributes = $resource;
    		if ( isset( $resource['href'] ) ) {
    			$href = $resource['href'];
    			if ( isset( $unique_resources[ $href ] ) ) {
    				continue;
    			}
    			$unique_resources[ $href ] = $attributes;
    			// Media can use imagesrcset and not href.
    		} elseif ( ( 'image' === $resource['as'] ) &&
    			( isset( $resource['imagesrcset'] ) || isset( $resource['imagesizes'] ) )
    		) {
    			if ( isset( $unique_resources[ $resource['imagesrcset'] ] ) ) {
    				continue;
    			}
    			$unique_resources[ $resource['imagesrcset'] ] = $attributes;
    		} else {
    			continue;
    		}
    	}

    	// Build and output the HTML for each unique resource.
    	foreach ( $unique_resources as $unique_resource ) {
    		$html = '';

    		foreach ( $unique_resource as $resource_key => $resource_value ) {
    			if ( ! is_scalar( $resource_value ) ) {
    				continue;
    			}

    			// Ignore non-supported attributes.
    			$non_supported_attributes = array( 'as', 'crossorigin', 'href', 'imagesrcset', 'imagesizes', 'type', 'media', 'fetchpriority' );
    			if ( ! in_array( $resource_key, $non_supported_attributes, true ) && ! is_numeric( $resource_key ) ) {
    				continue;
    			}

    			// imagesrcset only usable when preloading image, ignore otherwise.
    			if ( ( 'imagesrcset' === $resource_key ) && ( ! isset( $unique_resource['as'] ) || ( 'image' !== $unique_resource['as'] ) ) ) {
    				continue;
    			}

    			// imagesizes only usable when preloading image and imagesrcset present, ignore otherwise.
    			if ( ( 'imagesizes' === $resource_key ) &&
    				( ! isset( $unique_resource['as'] ) || ( 'image' !== $unique_resource['as'] ) || ! isset( $unique_resource['imagesrcset'] ) )
    			) {
    				continue;
    			}

    			$resource_value = ( 'href' === $resource_key ) ? esc_url( $resource_value, array( 'http', 'https' ) ) : esc_attr( $resource_value );

    			if ( ! is_string( $resource_key ) ) {
    				$html .= " $resource_value";
    			} else {
    				$html .= " $resource_key='$resource_value'";
    			}
    		}
    		$html = trim( $html );

    		printf( "<link rel='preload' %s />\n", $html );
    	}
    }
    ```

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

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

 [apply_filters( ‘wp_preload_resources’, array $preload_resources )](https://developer.wordpress.org/reference/hooks/wp_preload_resources/)

Filters domains and URLs for resource preloads.

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

| Uses | Description | 
| [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 1 more](https://developer.wordpress.org/reference/functions/wp_preload_resources/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_preload_resources/?output_format=md#)

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

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

## User Contributed Notes

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