Are images slowing your website down? Boost theme performance with these tips!


Hi Reader,

our clients expect their websites to be fast.

And since Google introduced the Web Vitals, we now have a metric that measures performance. At least as far as Google's search and ad services are concerned.

Block themes include performance optimizations out of the box, like optimized CSS delivery (you can watch this video to learn more).

But there are still areas that need a knowledgeable developer to optimize them. And one of these is definitely images.

Images that are larger than they need to be are also heavier than needed.

So in this email we'll look at:

  1. What the default WordPress font sizes are.
  2. Why they are a bad fit for modern themes.
  3. What sizes to implement, and how.
  4. How to deal with existing content.

The default image sizes

By default, WordPress includes three image sizes:

  • Thumbnail: 150 x 150 pixels
  • Medium: 300 x 300 pixels
  • Large: 1024 x 1024 pixels

These sizes have been around for years. And they are not a great fit for most themes.

Users can customize these sizes through the Settings > Media screen.

But very few do, even more knowledgeable ones. So the default sizes often stay the default. And that's a problem.

What's the issue with these sizes?

Common widths for content areas are anywhere between 500 to 800 pixels, depending on the typeface and font size.

In practice this means that if users want to have an image that is as wide as their text content, they choose the large size.

And thereby they are inserting images that are 200 to 500 pixels to wide.

Now the width in and of itself is not an issue. Browsers handle the resizing, so that the image doesn't overflow.

What's an issue though is that every added pixel means a bigger image file. That not only means wasted band width. It also means worse performance in core metrics used by search engines.

The Twenty Twenty-Four theme demo site for example gets flagged for this by Google's Page Speed Insights:

Block themes have introduced another issue, and that is wide alignments. Wide sizes are often wider than the large size.

Applying a wide size to an image will stretch it to the requested size. So if you choose a smaller image, then it will look blurry.

So often users choose the original size instead. Which depending on the source image results in huge images being used.

How to solve these issues?

As you can guess by the problems outlined, the solution is to have two image sizes by default:

  1. One that is as wide as the content size in theme.json.
  2. One that is as wide as the wide size in theme.json.

That way users can choose the correct size for the width, and you get the best possible performance and display.

Of course it's possible for a website to have several widths. It's not uncommon for example for sales pages to have a wider content size than blog posts.

So here you can introduce a second set of image sizes that fit with these content and wide sizes.

What I like to do is re-use the medium and large sizes that are in WordPress:

  1. It results in a less clutter compared to adding more image sizes.
  2. It's more portable than custom image sizes.

Now I could set these sizes in the interface. But I rather prefer to filter these options in code:

Rather than keeping the user facing names, I change these as well to align with the new sizing. I also do remove the thumbnail size for users, which is a lot cleaner:

Dealing with existing content

A recurring issue with WordPress is that it stores URLs inside of the database. And images are no exception to that.

This is the markup of an image block that's stored in the database:

<!-- wp:image {"id":123,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large">
<img src="https://example.com/wp-content/uploads/image-1024x569.png" class="wp-image-6452"/>
</figure>
<!-- /wp:image -->

The image tag points to the image file at the moment of insertion.

In this case when the image was added to the editor, the large image size had a maximum width of 1024 pixels.

If you change the large image size, these changes are only reflected in images inserted after the change.

So what you need to do is:

  1. Find all images in the content using this size.
  2. Update the image source attribute with the new font size.

There are two possible solutions to this:

  1. Implement a filter on the content that changes the sizes on the fly (meaning during each render).
  2. Implement a WP CLI script that searches through all posts in the database, changes the image URLs, and saves the modified posts.

The role of dynamic images

Enterprise hosts like Altis Cloud, WordPress VIP or Servebolt have supported dynamic images for quite a while.

By default WordPress creates an image file for each registered size. Meaning that if you have one image and six sizes, this means seven image files.

Dealing with these images, as well as resizing them when needed, can be cumbersome.

The solution is to use dynamic images that are generated when needed. This has three advantages:

  1. You don't have to deal with tons of image files on your server.
  2. There's no need to generate new sizes when registering new images.
  3. Images are loaded off a dedicated CDN.

As of the time of this writing, the Jetpack Boost Image CDN (previously known as Jetpack Photon) is still available for free.

I'm not aware of any other standalone services that do offer on the fly image generation backed by a CDN. If you know one, please reply!

One lost word of caution: don't use on the fly image generation on the server.

Such scripts like TimThumb used to be all the rage back in the day. Image generation is resource intensive, so it will put a burden on your web server.

Therefore if you want to use such a feature make sure to offload it to a dedicated service.

What are your experiences?

As usual this email contains my experiences and knowledge. I don't claim to be a performance expert being what I need to achieve good scores on Web Vitals.

But if you are, and you got something valuable to share, let me know! And as usual, any comments or questions are also welcome.

Cheers,
Fränk

P.S. If you are a student of my Block Theme Academy course, you can find all the code snippets to implement these images inside of the Implementing performant images lesson.

There's also a code snippet to do on the fly image URL migrations in the Migrating image URLs on the fly lesson.

Fränk Klein from WP Development Courses

Every Sunday, I send out tips, strategies, and case studies designed to help agencies and freelancers succeed with modern WordPress. My goal is to go off the beaten path, and focus on sharing lessons learned from what I know best: building websites for clients. 100% free and 100% useful.

Read more from Fränk Klein from WP Development Courses

Hi Reader, summer time in Europe means holiday season. And that means lots of time spent traveling. So why not benefit from all that time you need to sit in a car, bus, train or plane, and learn something? Which is why I'm sharing four podcast episodes I recently listened to, and enjoyed a lot. A Deep Dive into Block Bindings, APIs, and the LSX Tour Operator Plugin with Ash Shaw In this episode of Woo DevChat, hosts Zach Stepek and Carl Alexander chat with Ash Shaw from Lightspeed about the...

Hi Reader, what you are reading now took me a long time to write. In fact I started over three times. The question I'm trying to answer is simple: What's it like to be a WordPress developer in 2024? And by extension of course trying to deduct the future from that. The challenge here is that I want to: Warn about the dangers for your career without wanting to create panic. Encourage you to grow your skills without sounding too negative about your current situation. It's not about Full-Site...

Hi Reader, WordPress has many useful blocks, some not-so-useful ones, and a few that really shouldn't be there anymore. Like the Spacer block. Although the Spacer Block might seem like a simple solution for adding space to pages, it has several drawbacks that can negatively impact your website's performance, design, and accessibility. Understanding the Spacer Block The Spacer block shipped with WordPress 5.0, released on December 6, 2018. Back then, it was the only block-based option for...