When you upload an image to the WP Media Library, all the "neccessary" thumbnails (of various sizes and scales) are automatically generated, depending on your WP site settings and the theme you are using.
I use reGenerate Thumbnails Advanced to regenerate all of the current images on my site and remove all of the thumbnails. To prevent the generation when you upload new images in the future, you can use the Stop Generating Unnecessary Thumbnails plugin.
When you upload an image to the WP Library, this image can be very large (in MB). It's recommended to resize and compress the image before uploading it to your site, but what about the images you have already uploaded?
I use
imagemagick to shrink the images to a maximum width and height of 1280x1080 and skip images that are below this value.Resize all the images in the current folder and put the new images in
./out/In order to compress the images, you can use iloveimg website.
If WP Amin > Settings > Media > "Organize my uploads into month- and year-based folders" is enabled (normally this option is enabled by default), every time you upload a new image, it will be placed in a subfolder like
2022/09/21/. So, if you want to find a featured image of a post to resize and compress it, you need to know which folder it belongs to.Use the following Python script to move all images in subfolders to a new folder,
How to use it?
Require: posts are already attached with an featured image name.
This is useful when doing the previous step (moving all featured images from subfolders to a new folder). Each post has the featured image's information attached (the name and the path of the image). If you change the path of this image (on disk), you must also change the path in the post's data. We can do this by the name of the image and the title of the post.
Uncheck "Origanize my uploads in month- and year-based folders" in WP Admin > Settings > Media.
Get the list of posts with their data: WP Admin > GraphiQL IDE,
Note that WPGraphQL is limited to a maximum of 100 nodes by default (ref), follow this guide to increase that limit.
After retrieving the post data, you can save it in a
posts.json file.We need wp-cli to attach an image to a post and set it as the feature image for that post.
If you are using Local, the wp-cli is already there (you just need to go to the site folder on your terminal or using Local > "Open site shell"). Otherwise, install wp-cli.
The following script is an example. It automatically detects the name of the featured image attached to a post (in a list of posts
posts.json) and attaches that image from the uploads_resize_path folder to that post with a new path.Use it?