apply_filters( 'wp_get_attachment_image_attributes', array $attr , WP_Post $attachment , string|array $size )
Filters the list of attachment image attributes.
Description
Parameters
- $attr
-
(array) Attributes for the image markup.
- $attachment
-
(WP_Post) Image attachment post.
- $size
-
(string|array) Requested size. Image size or array of width and height values (in that order). Default 'thumbnail'.
Source
File: wp-includes/media.php
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
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.Expand full source codeCollapse full source code
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
You can add custom data attributes to image elements by adding this snippet on theme’s function.php:
Expand full source codeCollapse full source code
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).