apply_filters( 'wp_get_attachment_image_attributes', string[] $attr , WP_Post $attachment , string|int[] $size )
Filters the list of attachment image attributes.
Parameters
-
$attr
string[] -
Array of attribute values for the image markup, keyed by attribute name.
See wp_get_attachment_image() .More Arguments from wp_get_attachment_image( ... $attr )
Attributes for the image markup.
src
stringImage attachment URL.class
stringCSS class name or space-separated list of classes.
Defaultattachment-$size_class size-$size_class
, where$size_class
is the image size being requested.alt
stringImage description for the alt attribute.srcset
stringThe'srcset'
attribute value.sizes
stringThe'sizes'
attribute value.loading
string|falseThe'loading'
attribute value. Passing a value of false will result in the attribute being omitted for the image.
Defaults to'lazy'
, depending on wp_lazy_loading_enabled() .decoding
stringThe'decoding'
attribute value. Possible values are'async'
(default),'sync'
, or'auto'
. Passing false or an empty string will result in the attribute being omitted.
-
$attachment
WP_Post -
Image attachment post.
-
$size
string|int[] -
Requested image size. Can be any registered image size name, or an array of width and height values in pixels (in that order).
Source
File: wp-includes/media.php
.
View all references
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
This only seems to fire for featured images. At least, it’s the experience of this person on Stack Exchange, and of myself.
https://wordpress.stackexchange.com/questions/266702/wp-get-attachment-image-attributes-not-working-for-me
Add a data attribute to each <img> tag in a gallery
Note: this filter runs inside
wp_get_attachment_image()
, which means your attribute will be added for all uses of the function.You can add custom data attributes to image elements by adding this snippet on theme’s function.php:
Also, note that I use
array_key_exists()
PHP function instead ofisset()
becauseisset()
does not return TRUE for array keys that correspond to a NULL value, whilearray_key_exists()
does as of PHP Manual (http://php.net/manual/en/function.array-key-exists.php).