Can I refresh the thumbnails programmatically?
-
16-10-2019 - |
Question
I would like to be able to refresh the thumbnail cache programmatically, not sure where to hook it, but at present any design changes mean re-uploading loads of images!
Solution
You may want to look at the plugin Regenerate Thumbnails by Viper007Bond.
Basically, this is how to do it:
function regenerateThumbnails() {
$images = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%'" );
foreach ( $images as $image ) {
$id = $image->ID;
$fullsizepath = get_attached_file( $id );
if ( false === $fullsizepath || !file_exists($fullsizepath) )
return;
if ( wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $fullsizepath ) ) )
return true;
else
return false;
}
}
Note: This function is not very scalable. It will loop through all the images and regenerate thumbnails one by one, which may consume a large amount of memory. So, you may want to enhance it.
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange