Is there a way to append the image id class to existing attached images in the post content automagically with a php function? That would be incredibly awesome.

Here is what I have so far. It seems like it should work, in theory. What am I doing wrong?

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 ( current_user_can( 'manage_options' ) && is_admin()){
function my_update_posts() {

        $args = array(
        'post_type' => 'post',
        'numberposts' => -1,
        'category' => 1,
        'ID' => 1,

    $myposts = get_posts($args);
    foreach ($myposts as $mypost){
        $content = $mypost->post_content;
        apply_filters('be_attachment_id_on_images', $content);
        $mypost->post_content = $content;
        wp_update_post( $mypost );


add_action( 'admin_init', 'my_update_posts' );

  • Did you find a working solution for this? I'm running into the same issue after importing some posts from another platform into WordPress.
    – Mark B
    Aug 12 at 17:56

Shamelessly copied from here: https://letswp.io/add-attachment-id-to-the-class-of-wordpress-images/

add_filter( 'wp_get_attachment_image_attributes', 'add_image_id_class', 10, 2 );

function add_image_id_class( $attr, $attachment ) {

    $class_attr = isset( $attr['class'] ) ? $attr['class'] : '';

    $has_class = preg_match( '/wpimage\_[0-9]+/', $class_attr, $matches );

    if ( !$has_class ) {

        $class_attr .= sprintf( ' wpimage_%d', $attachment->ID );

        $attr['class'] = ltrim( $class_attr );


    return $attr;


This add a class like wpimage_{ATTACHMENT_ID} for example wpimage_22.


According to this earlier answer, the wp_get_attachment_image_attributes filter only works for the Featured Image/Post Thumbnail, although I'm not sure if the assertion is correct. Other places claim it doesn't work with the block editor. You might try the get_image_tag_class() filter. Or, depending on your goals, it might suffice to use a slightly different approach: add a class to the body (using the body_class() filter), then use a css selector like body.special img (that doesn't give the ID, though).

  • Hi Loren, my goal is to be able to eventually use WP's built-in responsive images with the images hardcoded in the posts that do not currently have the attachment IDs. Is there another way to achieve this without the IDs?
    – SHA3.org
    Mar 9 '19 at 5:29
  • Let me see if I understand your goal correctly. You have a bunch of posts, which link to images (or perhaps also reference some via <img> tags?), but the images are not part of the usual media library. The goal is to support responsive variants of the images: create <img> tags with the 'srcset' attribute, and on the server pre-create image variants that match. Right? And the question about adding css classes is just a sideline to that -- because one way might be to add them to the media library, and add parallel css classes (for consistency with other uses of images already in the library). Mar 11 '19 at 5:13
  • 1
    The images are already sized with multiple variants inside the media library and are attached to the posts. The issue is that the html inside the content does not include the IDs because they were inserted as part of an import without the image IDs known before the import, so in order to use these variant image sizes dynamically, I would need to know the attachment IDs and include them in the html of the content. I keep thinking there should be some built-in WP feature that recognizes the attached image IDs without them having to be declared explicitly in the html...
    – SHA3.org
    Mar 11 '19 at 20:13
  • 1
    Would attachment_url_to_postid() help? Thought I'd mentioned that, but maybe not. (Note though that I think you have to have a reference to the original image, not a variant. Looks like it is, however, smart enough to deal with 'relative' URLs that don't explicitly give a domain.) If need be, make a local copy of that function and add error log calls so you can trace it's behavior.... Mar 11 '19 at 21:11
  • 1
    ...An alternative, if the images are already set as attachments to the posts, would be to call get_attached_media(), and simply iterate though the result. Mar 11 '19 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.