I have a few posts that require their thumbnail sizes to be 1000x430. If I register this size and regenerate the thumbnails, it will create this size for all images in the media folder, if I'm not mistaken. If that's the case, that would be way too many 1000x430 images I don't need. Is there a way to only create this thumbnail size for the images I want?
1 Answer
Yes, you can choose to regenerate the thumbnails for specific images. You have 2 options.
1- Create a simple plugin
Generating thumbnails is done via the wp_generate_attachment_metadata()
function, which you can use to generate thumbnails manually. To do so, make a simple plugin that loops through a couple of attachment IDs and generates thumbnail for them. Here's what you need:
<?php
/**
* Plugin Name: Generate Thumbnails
* Plugin URI: http://example.com/
* Description: A plugin to generate some thumbnails.
* Version: 1.0
* Author: Desi
* Author URI: http://example.com
* Text Domain: desi
*
*/
// Trigger our function once the plugin has been activated
register_activation_hook( __FILE__, 'wpse335041_regenerate_thumbnails' );
function wpse335041_regenerate_thumbnails(){
// This is an array of attachment ids
$attachment_ids = [ 14, 490, 77, 129 ];
// Loop through the ids and generate thumbnail for each
foreach ( $attachment_ids as $id ) {
// Check if the attachment is an image
if( wp_attachment_is_image( $id ) ){
// Get the attachment path
$attachment = get_attached_file( $id );
if( $attachment ) {
// Generate thumbnails and metadata
wp_generate_attachment_metadata( $id, $attachment );
}
}
}
}
All you have to do now is to activate the plugin. If there are lots of images that need regeneration, it might take a while.
2- Use a plugin
There's a well-known plugin in the plugin repository that can be used to regenerate the thumbnails for a single or multiple image. You can download the plugin via the plugin repository here. Notice that I'm not the plugin's author.