2

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
  • 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 '17 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 '17 at 15:44
  • I'm going to do a completely vanilla install to see if those filters actually work... – LubosB May 12 '17 at 15:51
4

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.

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.