Support » Plugin: Regenerate Thumbnails » [Plugin: Regenerate Thumbnails] Patch to allow regenerating thumbnails per month

  • If a site has a large number of images (e.g. one of mine has > 20k), using regenerate-thumbnails to batch resize all images may take a looong time. So I wrote a patch to allow the user to enter a month, so that he only resizes images for a certain month. This way it can be more controlled.

    Here is the SVN diff:

    Index: regenerate-thumbnails.php
    ===================================================================
    --- regenerate-thumbnails.php	(revision 505003)
    +++ regenerate-thumbnails.php	(working copy)
    @@ -164,7 +164,18 @@
     				// Directly querying the database is normally frowned upon, but all
     				// of the API functions will return the full post objects which will
     				// suck up lots of memory. This is best, just not as future proof.
    -				if ( ! $images = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' ORDER BY ID DESC" ) ) {
    +
    +				// If a month was selected, let's validate it and plug it into query.
    +				// The month date should be in format YYYY/MM
    +				if ( ! empty( $_POST['regenerate-thumbnails-date'] ) ) {
    +					if ( preg_match( '/(\d{4})\/(\d{2})/', $_POST['regenerate-thumbnails-date'], $date_parts ) ) {
    +						$month = $date_parts[1] . '/' . $date_parts[2];
    +					}
    +				}
    +				// Next, build the SQL to inject into query
    +				$where_date = !empty( $month ) ? "AND DATE_FORMAT(post_date, '%Y/%m') = '$month'" : "";
    +
    +				if ( ! $images = $wpdb->get_results( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'image/%' $where_date ORDER BY ID DESC" ) ) {
     					echo '	<p>' . sprintf( __( "Unable to find any images. Are you sure <a href='%s'>some exist</a>?", 'regenerate-thumbnails' ), admin_url( 'upload.php?post_mime_type=image' ) ) . "</p></div>";
     					return;
     				}
    @@ -327,6 +338,10 @@
    
     	<p><?php _e( 'To begin, just press the button below.', 'regenerate-thumbnails '); ?></p>
    
    +	<p>
    +		<?php _e( 'Month to regenerate, in format YYYY/MM (leave blank to include all images)', 'regenerate-thumbnails' ); ?>
    +		<input type="textfield" class="hide-if-no-js" name="regenerate-thumbnails-date" id="regenerate-thumbnails-date" value="<?php print date('Y/m'); ?>" />
    +	</p>
     	<p><input type="submit" class="button hide-if-no-js" name="regenerate-thumbnails" id="regenerate-thumbnails" value="<?php _e( 'Regenerate All Thumbnails', 'regenerate-thumbnails' ) ?>" /></p>
    
     	<noscript><p><em><?php _e( 'You must enable Javascript in order to proceed!', 'regenerate-thumbnails' ) ?></em></p></noscript>
    @@ -396,4 +411,4 @@
     	$RegenerateThumbnails = new RegenerateThumbnails();
     }

    http://wordpress.org/extend/plugins/regenerate-thumbnails/

Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Plugin: Regenerate Thumbnails] Patch to allow regenerating thumbnails per month’ is closed to new replies.