Skip to content

Instantly share code, notes, and snippets.

@ivanhoe011
Last active March 5, 2018 00:27
Show Gist options
  • Save ivanhoe011/1cea8d5f176d73e2fd5f to your computer and use it in GitHub Desktop.
Save ivanhoe011/1cea8d5f176d73e2fd5f to your computer and use it in GitHub Desktop.
Resize all thumbs in WP
<?php
/**
* Resize all thumbs in WP when thumbnail settings are updated
*/
function update_thumbnails() {
set_time_limit(0); // can help if lot of images
$attachments = get_children( array('post_type' => 'attachment', 'post_mime_type' => 'image') );
if ( !$attachments )
return;
foreach ( $attachments as $id => $attachment ) {
$imagedata = wp_get_attachment_metadata($id);
$thumbnail = dirname($imagedata['file']) . '/' . $imagedata['sizes']['thumbnail']['file'];
if ( file_exists($thumbnail) )
@unlink($thumbnail);
wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $imagedata['file']));
}
}
add_action('update_option_thumbnail_size_w', 'update_thumbnails');
add_action('update_option_thumbnail_size_h', 'update_thumbnails');
add_action('update_option_thumbnail_crop', 'update_thumbnails');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment