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:
- Return a remote file from the cache when it’s available.
- 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()
).
https://gist.github.com/danielbachhuber/5cf816d99fd973cd5832a5715576ade8
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()
:
https://gist.github.com/danielbachhuber/aad495d331f45d4a0ecd97f9f243b9e6
If you’d like to generate image thumbnails later on, simply use wp media regenerate
(doc).