One of my favorite features of Synthesis, the host I use as my development server, is that it has WP-CLI pre-installed (here’s some other hosts that have it as well). It has a ton of features I use every day, but the one I’m going to highlight today is regenerating images.
When you’re building a client’s site, you’ll often need to create a new image size for a certain area of the site. You’ll then need to regenerate thumbnails so that already uploaded images have this new image size available. To regenerate all images, you simply type this: wp media regenerate
But if you’re working on a site with a lot of content, that might take a long time to run. There might only be one image that’s using this new image size, so let’s just regenerate that one and move on. If you type the attachment ID (or multiple IDs) at the end of that line, it will just regenerate those.
Most WordPress themes will have a class added to images, “wp-image-xxxx”, where xxxx is the attachment ID. When you inspect the image, you can quickly see the ID:
Genesis 2.0 replaces the image classes with its own, but here’s some code that adds it back:
<?php | |
/** | |
* Attachment ID on Images | |
* | |
* @author Bill Erickson | |
* @link http://www.billerickson.net/code/add-attachment-id-class-images/ | |
*/ | |
function be_attachment_id_on_images( $attr, $attachment ) { | |
if( !strpos( $attr['class'], 'wp-image-' . $attachment->ID ) ) | |
$attr['class'] .= ' wp-image-' . $attachment->ID; | |
return $attr; | |
} | |
add_filter( 'wp_get_attachment_image_attributes', 'be_attachment_id_on_images', 10, 2 ); | |
If I created a new image size and that’s the image I wanted to test, I’d type: wp media regenerate 3888
.
Quick and easy!
Maria Mincey says
Great post.
Very useful and easy method to regenerate images using WP-CLI