Ready to get started?Download WordPress


Regenerate Thumbnails
Patch to allow regenerating thumbnails per month (7 posts)

  1. krisahil
    Posted 2 years ago #

    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>";
    @@ -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();


  2. lordkain2
    Posted 2 years ago #

    Can you please explain me how to apply the patch?

    Thanks for sharing.

  3. krisahil
    Posted 2 years ago #


    Put the following into a file named "add_per_month.diff" and copy that file into the regenerate-thumbnails plugin folder. Then go to that folder and run:
    patch -p0 -i add_per_month.diff

    Here is the content for that file:
    [Code moderated as per the Forum Rules. Please use the pastebin]

  4. lordkain2
    Posted 2 years ago #

    Can you send me the patched file please? I don't have access to the shell.

  5. krisahil
    Posted 2 years ago #

    My bad about the patch. Here is the :
    full patch on pastebin.

    @lordkain2, I cannot send you the modified file. However, maybe you could contain the maintainer of this plugin to see if he'll include my patch in a future version? Or, another option is for you to download the file via FTP, patch it on your local computer, then upload the modified version back to your site. That should do it, too.

  6. lordkain2
    Posted 2 years ago #

    Thanks!!!! Works like a charm.

  7. Sebastian Cork
    Posted 2 years ago #

    Anyone not running Linux or that has SSH may be interested in the GNUWin Patch for Windows utility.


Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic