4

I've distilled this example down to its most basic functionality on my local site and it still has no effect, no matter which attribute I choose to change.

Here is the developer page for the filter, with a couple of examples. There's also a working example inside functions.php in the TwentySixteen theme. I've read a few examples on this stack as well, and by all accounts this should work but it just doesn't change the attribute:

function developing_filter_img_attr( $attr ) {

    $attr['class'] = "none";

    return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'developing_filter_img_attr' );

Am I missing something? Is it a priority issue? I have a few other filters within my functions file, all dealing with images and their attributes, and they've all worked correctly and continue to work correctly. They include:

  • intermediate_image_sizes_advanced
  • max_srcset_image_width
  • wp_calculate_image_sizes
3
  • Did you try with a different priority (e.g. 999) or did you test it on a vanilla setup with the TwentySixteen theme ?
    – birgire
    May 12, 2017 at 15:26
  • @birgire Interesting, I'm actually not seeing the changes that both TwentySixteen and TwentySeventeen are trying to make using this same filter... They both try to change the sizes attribute on certain conditions, but when I inspect the images, these custom attributes aren't showing. I think they're still using the default WP sizes setting.
    – LubosB
    May 12, 2017 at 15:44
  • I'm going to do a completely vanilla install to see if those filters actually work...
    – LubosB
    May 12, 2017 at 15:51

1 Answer 1

10

Okay, the problem was in my interpretation of the filter. I was under the impression that this filter could be used for all images in a post, as they're all considered attachments in the db (post type - attachment), but it's actually reserved for the Featured Image/Post Thumbnail. Nowhere in the examples I looked through was this explicitly mentioned...

My mistake, but I wish the info online was more clear about this and the name of the filter had something to do with post_thumbnail or featured image.

1
  • Have you managed to solve that back then somehow :)? I have the same problem. The only solution that i have for now is simply hardcoding the images in the html that i want with custom srcset. Yet into searching for hooks, filters that can be used for generic images (and not only featured images but any, whether they added by Blocks, Gutenberg or Classic editor or whatever). Anyone ideas? Jun 1, 2022 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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