0

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' );

}
0

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? – alice Mar 9 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). – Loren Rosen Mar 11 at 5:13
  • 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... – alice Mar 11 at 20:13
  • This is the current html format of the attached images in the content. If I manually add the image ID, everything works. But I have thousands of posts, and manually adding the IDs is not feasible. <img class="myimages" src="/wp-content/uploads/image.png"/> – alice Mar 11 at 20:20
  • 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.... – Loren Rosen Mar 11 at 21:11

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.