Filter Hook since 2.8.0
wp_get_attachment_image_attributes
Filters the HTML attributes of an image rendered by wp_get_attachment_image()
Description
wp_get_attachment_image_attributes touches every image WordPress renders through its own media functions — including inside blocks and thumbnails — so scope changes carefully.
When it runs
Any time WordPress builds an image tag through its attachment image functions — featured images, block images, media library output.
Signature
apply_filters( 'wp_get_attachment_image_attributes', array $attr, WP_Post $attachment, string|array $size );Parameters
$attrarray — Image HTML attributes.$attachmentWP_Post — Attachment post object.$sizestring|array — Requested image size.
Examples
Basic
add_filter( 'wp_get_attachment_image_attributes', function( $attr ) {
$attr['loading'] = 'lazy';
$attr['decoding'] = 'async';
return $attr;
} );Force lazy-loading and async decoding on every attachment image.
Common Use Cases
- Add loading="lazy" or fetchpriority attributes
- Override alt text site-wide
- Add custom data-* attributes for a lightbox script
Common mistakes
- Overriding src/srcset without preserving responsive sizes — can break responsive image behavior added since WP 4.4
Related hooks
FAQ
Does this filter affect images pasted as raw HTML in the editor?
No — only images rendered through WordPress's own attachment image functions (wp_get_attachment_image and friends), not raw <img> HTML.
Source: wp-includes/media.php