apply_filters( ‘wp_preload_resources’, array $preload_resources )

Filters domains and URLs for resource preloads.

Parameters

$preload_resourcesarray
Array of resources and their attributes, or URLs to print for resource preloads.
  • ...$0 array
    Array of resource attributes.
    • href string
      URL to include in resource preloads. Required.
    • as string
      How the browser should treat the resource (script, style, image, document, etc).
    • crossorigin string
      Indicates the CORS policy of the specified resource.
    • type string
      Type of the resource (text/html, text/css, etc).
    • media string
      Accepts media types or media queries. Allows responsive preloading.
    • imagesizes string
      Responsive source size to the source Set.
    • imagesrcset string
      Responsive image sources to the source set.

Source

$preload_resources = apply_filters( 'wp_preload_resources', array() );

Changelog

VersionDescription
6.1.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    This is how you could use the wp_preload_resources filter to preload your theme styles.

    function craftedat_preload_assets( array $preload_resources ): array {
    
    	/**
    	 * Check if stylesheet is loaded.
    	 * 'theme-style' is the WP handle of your stylesheet.
    	 */
    	if ( wp_style_is( 'theme-style' ) ) {
    		$wp_style_theme = wp_styles()->registered['theme-style'];
    
    		// Including the version number is crucial.
    		$path_to_theme_styles = $wp_style_theme->src . '?ver=' . $wp_style_theme->ver;
    
    		$preload_resources[] = array(
    			'href'  => $path_to_theme_styles,
    			'as'    => 'style',
    			'media' => 'all',
    			'type'  => 'text/css',
    		);
    	}
    
    	return $preload_resources;
    }
    
    add_filter( 'wp_preload_resources', 'craftedat_preload_assets' );

You must log in before being able to contribute a note or feedback.