WordPress Speed Optimization

WordPress Image Optimization: WebP, AVIF, Sizes and Lazy Loading

What WordPress already does with every image, how to choose between JPEG, WebP and AVIF, how to get srcset and lazy loading right, and how to repair a media library that is already full.

WordPress Image Optimization: WebP, AVIF, Sizes and Lazy Loading

WordPress does more for images than most people realize. Every upload is cut into several sizes, and every image placed through the editor gets srcset, dimensions and lazy loading without anyone asking. When images are still the slowest thing on the page, the cause is usually a file that was too big to begin with, a format left at JPEG or PNG, or a theme or page builder that goes around what core provides.

Good WordPress image optimization is mostly a matter of checking four things in order: the format, the pixel dimensions, the markup around the image and the loading priority.

What WordPress does with every upload

When a photo lands in the media library, WordPress generates a set of smaller copies: thumbnail, medium, medium large, large and two bigger sizes for high-density screens, plus any sizes the theme and plugins register. A photo wider or taller than 2,560 pixels is also scaled down to that limit and saved with -scaled in its file name, and that copy is used as the full size. The original stays on disk.

When the image is output through a block or through wp_get_attachment_image(), the tag carries:

  • srcset and sizes, so the browser can choose a file that fits the screen
  • width and height, so space is reserved before the file arrives
  • loading="lazy" on images further down the page
  • fetchpriority="high" on the image core judges most likely to be the largest in the first screen

None of that applies to an image typed into a template as a bare <img> tag, or set as a CSS background in a builder. Those two cases account for most of the image problems we find in audits.

Choosing between JPEG, WebP and AVIF

FormatUse it forWatch for
JPEGPhotos, when nothing newer is availableThe largest files of the three for the same quality
WebPPhotos and graphics, with or without transparencyNeeds support in the server's image library
AVIFPhotos where the smallest file matters mostSlower to encode, and missing on some servers
PNGScreenshots and flat graphics with transparencyVery large when used for photos

WordPress accepts WebP and AVIF uploads and can resize them when the server's image library (Imagick or GD) supports the format. Check under Tools > Site Health > Info > Media Handling, which lists what your server handles. The dev note on AVIF support in WordPress explains the requirements.

One point surprises people: WordPress keeps the format you upload. A JPEG upload produces JPEG sizes. To have core write the generated sizes in a newer format, a developer adds a filter:

add_filter( 'image_editor_output_format', function ( $formats ) {
    $formats['image/jpeg'] = 'image/webp';
    return $formats;
} );

That affects new uploads only. The alternatives are a conversion plugin, which converts the existing library and serves the newer format to browsers that accept it, or an image CDN that converts on the fly. Any of the three is reasonable, as long as the original is kept.

Upload sensible pixel dimensions

No format rescues a 6,000 pixel camera file. Work out the widest an image is ever displayed in your layout, double it for high-density screens and export at that width. A full-width hero rarely needs more than about 2,500 pixels, and an image inside a text column needs far less.

Compression matters as much. Export photos at a quality where you cannot see the difference at normal size, and never use PNG for photographs. WordPress compresses the sizes it generates at a default quality, which a developer can change with the wp_editor_set_quality filter.

Make srcset and sizes tell the truth

srcset lists the available files and their widths. sizes tells the browser how wide the image will appear, and the browser picks a file using both. If sizes is wrong, the browser downloads the wrong file however many sizes exist.

By default WordPress writes a sizes value that assumes the image fills the viewport up to its own width, for example (max-width: 1024px) 100vw, 1024px. That is right for a full-width image. In a three-column grid on desktop, every card downloads a file meant for a slot three times as wide. The fix belongs in the theme: pass an accurate value where the image is output, or filter wp_calculate_image_sizes.

echo wp_get_attachment_image( $image_id, 'large', false, array(
    'sizes' => '(min-width: 1024px) 33vw, (min-width: 640px) 50vw, 100vw',
) );

Current WordPress versions also add auto to the sizes of lazy-loaded images, which lets supporting browsers use the real layout width. Images at the top of the page are not lazy-loaded, so they still depend on an honest value. The responsive images documentation lists the functions and filters involved.

Lazy load everything except the first screen

Lazy loading delays an image until the visitor scrolls near it, and WordPress applies it to images and iframes by default. Applied to the image at the top of the page it backfires, because the browser waits for layout before it requests the file that decides your Largest Contentful Paint.

Core handles this by leaving the first few content images out of lazy loading and giving the likely LCP image a high fetch priority. Check that nothing has undone it. View the source of a key page, find the hero and confirm that:

  1. it is an <img> element and not a CSS background
  2. it has no loading="lazy" attribute
  3. its src is a real file address, not a placeholder that a script swaps later
  4. it carries fetchpriority="high", and no other image on the page does

The usual offenders are optimization plugins with their own JavaScript lazy loader switched on for every image, sliders, and headers where the hero is a background. Exclude the hero and the logo from plugin lazy loading. If the hero has to stay a background, have the theme preload it. How this feeds the metric is covered in Core Web Vitals for WordPress.

Width and height keep the page still

When an image has width and height attributes, the browser reserves the right box before the file arrives and nothing below it moves. Core adds them for media library images. They go missing on hard-coded images, on SVG logos and in some builder widgets, and the page jumps as each one pops in.

The CSS matters too. Keep height: auto on responsive images so the reserved box scales with the layout, and set aspect-ratio on containers that hold a background image or an embed.

Repairing the library you already have

Changing the theme, registering a new image size or switching formats does nothing for files uploaded earlier. Their sizes have to be regenerated. With WP-CLI:

wp media regenerate --only-missing
wp media regenerate --image_size=large --yes

The first command creates only the sizes that do not exist yet. The second rebuilds one named size for every image. Take a backup and check free disk space before a full run, because a big library multiplied by a dozen registered sizes adds up quickly. If the theme and plugins register sizes that nothing uses, have a developer remove them with the intermediate_image_sizes_advanced filter first.

On WooCommerce stores, start with the product images. Catalog thumbnails are requested more often than any other file on the site.

Which parts of WordPress image optimization need a developer

Formats, upload dimensions, a conversion plugin and regeneration are settings and housekeeping. A site owner or content editor can handle them, and they usually deliver most of the saving.

An accurate sizes value, a hero that is a real prioritized image, missing dimensions and unused image sizes are code changes in the theme or in a small plugin. If an audit keeps flagging the same images after the housekeeping is done, the markup is the cause and a developer is the right person for it. We handle that as part of performance optimization work, and when the theme resists at every step, as a custom theme build.

Frequently asked questions

Should I use WebP or AVIF on WordPress?

WebP is the safe default. Every current browser displays it and most hosts can generate it. AVIF files are usually smaller again for photos, but encoding is slower and some servers lack support, so check Site Health before you commit to it.

Does WordPress convert JPEG uploads to WebP automatically?

No. Core keeps the format of the uploaded file for the sizes it generates. Conversion happens only when a developer adds the output format filter, or when a plugin or an image CDN does it.

Do I still need an image optimization plugin?

Not always. If you upload well-sized WebP files and the theme outputs images through core functions, WordPress covers responsive sizes and lazy loading by itself. A plugin earns its place when editors upload whatever comes off the camera, or when a large existing library needs converting.

Why does PageSpeed Insights still say my images are too large?

Usually because the sizes attribute overstates how wide the image is displayed, so the browser picks a bigger file than the slot needs. The other common cause is an image used as a CSS background, which gets no responsive sizes at all. Both are fixed in the theme markup, not in the media library.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

Swati Priyadrshni
· Founder & CEO at WorldWin Coder

I'm Swati Priyadarshani, Founder & CEO of LMS Advisor and Co-Founder of WorldWin Coder Pvt. Ltd. (est. 2019). Over the last 10+ years, I've helped enterprises build learning platforms that actually get used - not just deployed.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.