Title: image preloader
Last modified: March 31, 2025

---

# image preloader

 *  Resolved [dingonobru](https://wordpress.org/support/users/dhumeurvegetale/)
 * (@dhumeurvegetale)
 * [1 year, 2 months ago](https://wordpress.org/support/topic/image-preloader/)
 * Hi, I’m using your plugin a lot and it works fine, thank you. I’ve got a problem
   with an image preloader snippet i wrote… if i mention the attribut **as=image**
   the code break and if i don’t mention it the dev-tool say it needs a **as** attribut…
 * Here’s my code:
 * function humveg_imgs_preloader() {
 * if( ! is_front_page() ) return;
 * $link = ”;
 * foreach( array(‘2024/03/bg-mocktails-min.webp’ , ‘2024/03/bg-smoothie-milkshake-
   min.webp’ , ‘2024/03/bg-detox-water-min.webp’) as $src) {
 *     ```wp-block-code
       $link.='<link rel="preload" fetchpriority="high" as="image" href="/wp-content/uploads/'. $src .'" type="image/webp">' . "\n";
       ```
   
 * }
   echo $link;
 * }
 * add_action( ‘wp_head’, ‘humveg_imgs_preloader’ );
 * without the **as** attribut it is print in the head section but with it even 
   don’t print and break the code.
 * Maybe you have already had this question or knows an issue for me.
 * Thanks a lot
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fimage-preloader%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Yordan Soares](https://wordpress.org/support/users/yordansoares/)
 * (@yordansoares)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/image-preloader/#post-18417976)
 * Hi [@dhumeurvegetale](https://wordpress.org/support/users/dhumeurvegetale/),
 * It seems that the code is broken. Could you please try sharing it again within
   a “Code” block?
 * Just click on the blue icon with the plus symbol (+) within the editor, then 
   search the “Code” block, and finally paste your code there 😉
 *  Thread Starter [dingonobru](https://wordpress.org/support/users/dhumeurvegetale/)
 * (@dhumeurvegetale)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/image-preloader/#post-18418395)
 *     ```wp-block-code
       function humveg_imgs_preloader() {	  if( ! is_front_page() ) return;	  $link = '';		  foreach( array('2024/03/bg-mocktails-min.webp' , '2024/03/bg-smoothie-milkshake-min.webp' , '2024/03/bg-detox-water-min.webp') as $src) {	  	$link.='<link rel="preload" as="image" fetchpriority="high"  href="/wp-content/uploads/'. $src .'" type="image/webp">' . "\n";	  }		echo $link;	}add_action( 'wp_head', 'humveg_imgs_preloader' );
       ```
   
 * Hi [@yordansoares](https://wordpress.org/support/users/yordansoares/) thanks 
   for your advice.
 * So these 3 images are on my website front page and declared like “Largest Contentful
   Paint” . The solution proposed is to preload this images but when i activate 
   the snippets the as=”image” the code is not printed at all in the head and if
   i don’t add the as=”image” it’s printed but the dev.tool say it need the as=”
   image” and images are not preloaded….
 * I don’t think it’s a problem with your plugin but with my code and because i’m
   not an expert i ask you for some advices….
 *  [Yordan Soares](https://wordpress.org/support/users/yordansoares/)
 * (@yordansoares)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/image-preloader/#post-18419586)
 * I’m afraid, this is not an issue related with Code Snippets, but with your code
   directly.
 * That said, I have updated your code a bit, as a courtesy. Please try this version
   instead:
 *     ```wp-block-code
       function humveg_imgs_preloader() {    if ( ! is_front_page() ) {        return;    }    $images = array(        '2024/03/bg-mocktails-min.webp',        '2024/03/bg-smoothie-milkshake-min.webp',        '2024/03/bg-detox-water-min.webp'    );    foreach ( $images as $src ) {        printf(            '<link rel="preload" as="image" fetchpriority="high" href="%s" type="image/webp" />' . "\n",            esc_url( get_site_url( null, '/wp-content/uploads/' . $src ) )        );    }}add_action( 'wp_head', 'humveg_imgs_preloader', 1 );
       ```
   
 *  Thread Starter [dingonobru](https://wordpress.org/support/users/dhumeurvegetale/)
 * (@dhumeurvegetale)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/image-preloader/#post-18419868)
 * Hi thanks for your help but it doesn’t work for me…. i tried already with the**
   printf **function; without the **as=image** it is printed on the source page:
 *     ```wp-block-code
       <link rel=preload fetchpriority=high href="https://dhumeurvegetale.lu/wp-content/uploads/2024/03/bg-mocktails-min.webp" type="image/webp"><link rel=preload fetchpriority=high href="https://dhumeurvegetale.lu/wp-content/uploads/2024/03/bg-smoothie-milkshake-min.webp" type="image/webp"><link rel=preload fetchpriority=high href="https://dhumeurvegetale.lu/wp-content/uploads/2024/03/bg-detox-water-min.webp" type="image/webp">
       ```
   
 * And the **dev.tool** says:
 *     ```wp-block-code
       <link rel=preload> must have a valid as valuedhumeurvegetale.lu/:9 <link rel=preload> must have a valid as valuedhumeurvegetale.lu/:10 <link rel=preload> must have a valid as value
       ```
   
 * But with the **as **attribut nothing is printed at all on the source page……
 * Thanks for your help anyway.
 *  [Yordan Soares](https://wordpress.org/support/users/yordansoares/)
 * (@yordansoares)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/image-preloader/#post-18423875)
 * That’s weird, [@dhumeurvegetale](https://wordpress.org/support/users/dhumeurvegetale/):
 * I just tested the code, and I’m seeing all the attributes as expected:
 * [[
 * Click to expand.
 * Therefore, If the `<link>` elements appear but the `as="image"` part is missing
   in the final HTML, it’s likely something external is modifying the output.
 * Here are a few steps to help diagnose and solve the issue:
    1. **Temporarily disable optimization or caching plugins** (like Autoptimize, LiteSpeed
       Cache, W3 Total Cache, etc.) and reload the page source. Check if the `as` attribute
       shows up when those plugins are turned off.
    2. **View the page source before minification/compression.** Some plugins alter
       HTML output only during optimization, so it’s worth checking the raw (unminified)
       version if possible.
    3. **Check for filters on `wp_kses_allowed_html`** or other sanitization functions.
       Some themes or plugins apply filters that remove attributes not explicitly allowed—
       and `as` might not be in their allowlist.
 * Hope it helps!
 *  Thread Starter [dingonobru](https://wordpress.org/support/users/dhumeurvegetale/)
 * (@dhumeurvegetale)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/image-preloader/#post-18424839)
 * Hi [@yordansoares](https://wordpress.org/support/users/yordansoares/) I deactivated**
   Assets Cleanup** and **LS Cache** but it doesn’t work with the as attribut….
 * I tried with the LS Cache debug mode to understand what append, but it’s to much
   information and the Debug Log Manager plugin said nothing about this case…
 * Can’t give more time for that, I’m closing the topic now and deactivate the snippet,
   Thanks for your help, anyway

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘image preloader’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

## Tags

 * [as](https://wordpress.org/support/topic-tag/as/)
 * [image](https://wordpress.org/support/topic-tag/image/)
 * [link](https://wordpress.org/support/topic-tag/link/)
 * [preload](https://wordpress.org/support/topic-tag/preload/)
 * [wp_head](https://wordpress.org/support/topic-tag/wp_head/)

 * 6 replies
 * 2 participants
 * Last reply from: [dingonobru](https://wordpress.org/support/users/dhumeurvegetale/)
 * Last activity: [1 year, 1 month ago](https://wordpress.org/support/topic/image-preloader/#post-18424839)
 * Status: resolved