Two performance tips for WordPress migrations

Images are a total bottleneck when importing a site into WordPress.

Your script’s execution time can easily double if your migration script fetches images over HTTP or generates multiple thumbnail sizes. Because you’ll likely need to test your script multiple times, these inefficiencies can cause substantial delays in your project.

Fortunately, two simple tricks will save you a ton of time.

Cache remote images

If you’ve already downloaded an image once, do you need to download it again and again? Probably not. You need a cache!

Drop the migrate-import-cache.php code snippet into your mu-plugins directory in order to:

  1. Return a remote file from the cache when it’s available.
  2. Write the contents of a remote file to cache for use on the next run.

You’ll need to set a WP_IMPORT_CACHE constant to a writable directory. Also, this code assumes you’re using media_sideload_url()(or some other function based on wp_remote_get()).

Disable thumbnail generation

WordPress generates a few to several thumbnails for every image uploaded into the Media Library. These thumbnails usually aren’t necessary during development, so having WordPress create them wastes CPU and your time.

To disable thumbnail generation, simply override the registered image sizes prior to calling media_sideload_image():

If you’d like to generate image thumbnails later on, simply use wp media regenerate (doc).

One Comment

Rasso December 27, 2021 Reply

Wow Daniel, I don’t even know how I found this post, but it’s exactly what I will need for an upcoming project with a huge amount of images to be imported. Thank you so much for this, and even more for your amazing work on WP-CLI! Cheers from another father from Berlin 🙂

Leave a Reply