Preload Images

By Ravikumar Patel

★ 4.6/5 (8 ratings)900+ active installationsVersion 1.3.0

Preload or prefetch images, site-wide or per page, for a faster page loading experience.

Description

Preload Images loads the images you choose before the visitor needs them. It works well for photo galleries, sliders, hover images and other image-heavy pages where you want images to appear instantly.

Features

  • Add any number of image URLs that load on every page.
  • Add image URLs for a single page, post or custom post type from the editor.
  • Block editor (Gutenberg) support: a “Preload Images” panel in the post sidebar, with an “Add from Media Library” button.
  • Choose how images are loaded:
    • Background (JavaScript) loads images after the page has finished loading. This is the default and does not slow down the first render.
    • Preload adds <link rel="preload" as="image"> to the page head. Use it only for a few important above-the-fold images, such as a hero image.
    • Prefetch adds <link rel="prefetch"> so the browser caches images at low priority for the next pages a visitor opens.
  • Lightweight: no external requests and no extra script files on the front end.
  • Developers can change the list with the preload_images_urls filter.

Installation

  1. In your dashboard, go to Plugins > Add New.
  2. Search for “Preload Images”.
  3. Click “Install Now”, then “Activate”.
  4. Go to the Preload Images menu, add your image URLs (one per line) and choose a loading method.
  5. To set images for single pages, tick the post types under “Post Types”. A “Preload Images” box then appears in the editor.

FAQ

Which loading method should I use?

Use Background (JavaScript) for most images. Use Preload only for one or two images that appear at the top of the page. Preloading many images makes the page slower. Use Prefetch for images that are shown on the next pages a visitor is likely to open.

Does it work with the block editor?

Yes. In the block editor, a “Preload Images” panel appears in the post sidebar, where you can type URLs or pick images from the Media Library. The classic editor shows a “Preload Images” box below the content.

The sidebar panel is used for post types that support custom fields (posts and pages by default). Other custom post types show the box below the content in both editors.

Can I change the URLs from code?

Yes:

add_filter( 'preload_images_urls', function ( $urls, $post_id ) { $urls[] = 'https://example.com/image.jpg'; return $urls; }, 10, 2 );

Screenshots

Changelog

1.3.0

  • New: block editor (Gutenberg) sidebar panel with Media Library picker.
  • New: choose a loading method: background JavaScript, <link rel="preload"> or <link rel="prefetch">.
  • New: preload_images_urls filter.
  • New: plugin data is removed when the plugin is deleted.
  • New: updated screenshots.
  • Fix: the “Settings” link on the Plugins screen pointed to the wrong page.
  • Fix: URLs containing & were double-encoded and could break. Existing saved URLs are cleaned up automatically on upgrade.
  • Fix: per-page images were not used on some archive pages.
  • Fix: per-page images of password-protected posts are no longer shown before the password is entered.
  • Improved: security hardening, escaping and coding standards.
  • Improved: the background script no longer adds global JavaScript variables and runs after the page has loaded.
  • Tested with WordPress 7.1. Requires WordPress 6.1 and PHP 7.4.

1.2.1

  • Bug fixes

1.2

  • Now you can manage images for single pages and post types.
  • PHP code changes.
  • Tested with WordPress 5.6.

1.1

  • Minor code changes.
  • Tested with WordPress 5.5.1.

1.0

  • Initial release.