Add Attachment ID as class on images

When developing a site, it can be useful to know the attachment ID of the image you’re looking at. I use it along with wp cli to regenerate specific thumbnails: wp media regenerate 1235

This adds a class containing the attachment ID to images, so you can right click and inspect element to know the appropriate ID.

<?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 );
view raw general.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk