Title: wp_get_attachment_image_attributes
Published: April 25, 2014
Last modified: August 20, 2026

---

# apply_filters( ‘wp_get_attachment_image_attributes’, string[] $attr, WP_Post $attachment, string|int[] $size )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#user-contributed-notes)

Filters the list of attachment image attributes.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#parameters)󠁿

 `$attr`string[]

Array of attribute values for the image markup, keyed by attribute name.
 See [wp_get_attachment_image()](https://developer.wordpress.org/reference/functions/wp_get_attachment_image/).

More Arguments from wp_get_attachment_image( … $attr )

Attributes for the image markup.

 * `src` string
 * Image attachment URL.
 * `class` string
 * CSS class name or space-separated list of classes.
    Default `attachment-$size_class
   size-$size_class`, where `$size_class` is the image size being requested.
 * `alt` string
 * Image description for the alt attribute.
 * `srcset` string
 * The `'srcset'` attribute value.
 * `sizes` string
 * The `'sizes'` attribute value.
 * `loading` string|false
 * The `'loading'` attribute value. Passing a value of false will result in the 
   attribute being omitted for the image.
    Default determined by [wp_get_loading_optimization_attributes()](https://developer.wordpress.org/reference/functions/wp_get_loading_optimization_attributes/).
 * `decoding` string
 * The `'decoding'` attribute value. Possible values are `'async'` (default), `'
   sync'`, or `'auto'`. Passing false or an empty string will result in the attribute
   being omitted.
 * `fetchpriority` string
 * The `'fetchpriority'` attribute value, whether `high`, `low`, or `auto`.
    Default
   determined by [wp_get_loading_optimization_attributes()](https://developer.wordpress.org/reference/functions/wp_get_loading_optimization_attributes/).

`$attachment`[WP_Post](https://developer.wordpress.org/reference/classes/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](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#source)󠁿

    ```php
    $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/media.php#L1282)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/media.php#L1282-L1282)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#related)󠁿

| Used by | Description | 
| [wp_get_attachment_image()](https://developer.wordpress.org/reference/functions/wp_get_attachment_image/)`wp-includes/media.php` |

Gets an HTML img element representing an image attachment.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.8.2](https://developer.wordpress.org/reference/since/6.8.2/) | The `$attr` array includes `width` and `height` attributes. | 
| [2.8.0](https://developer.wordpress.org/reference/since/2.8.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 5 content](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#comment-content-2569)
 2.    [Ian McDonald](https://profiles.wordpress.org/drianmcdonald/)  [  9 years ago  ](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/#comment-2569)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%23comment-2569)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%23comment-2569)
 4.  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](https://wordpress.stackexchange.com/questions/266702/wp-get-attachment-image-attributes-not-working-for-me)
 5.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%3Freplytocom%3D2569%23feedback-editor-2569)
 6.   [Skip to note 6 content](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#comment-content-61)
 7.    [Drew Jaynes](https://profiles.wordpress.org/drewapicture/)  [  12 years ago  ](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/#comment-61)
 8.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%23comment-61)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%23comment-61)
 9.  **Add a data attribute to each <img> tag in a gallery**
      _Note: this filter runs
     inside [`wp\_get\_attachment\_image()`](https://developer.wordpress.org/reference/functions/wp_get_attachment_image/),
     which means your attribute will be added for all uses of the function_.
 10.     ```php
         /**
          * Filter attributes for the current gallery image tag to add a 'data-full'
          * data attribute.
          *
          * @param array   $atts       Gallery image tag attributes.
          * @param WP_Post $attachment WP_Post object for the attachment.
          * @return array (maybe) filtered gallery image tag attributes.
          */
         function wpdocs_filter_gallery_img_atts( $atts, $attachment ) {
         	if ( $full_size = wp_get_attachment_image_src( $attachment->ID, 'full' ) ) {
         		if ( ! empty( $full_size[0] ) ) {
         			$atts['data-full'] = $full_size[0];
         		}
         	}
         	return $atts;
         }
         add_filter( 'wp_get_attachment_image_attributes', 'wpdocs_filter_gallery_img_atts', 10, 2 );
         ```
     
 11.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%3Freplytocom%3D61%23feedback-editor-61)
 12.  [Skip to note 7 content](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#comment-content-1664)
 13.   [leymannx](https://profiles.wordpress.org/leymannx/)  [  10 years ago  ](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/#comment-1664)
 14. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%23comment-1664)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%23comment-1664)
 15.     ```php
         /**
          * Add classes to the cutom logo.
          */
         add_filter( 'wp_get_attachment_image_attributes', function ( $attr ) {
     
         	if ( isset( $attr['class'] ) && 'custom-logo' === $attr['class'] ) {
         		$attr['class'] = 'custom-logo foo-bar NEW CLASSES HERE';
         	}
     
         	return $attr;
         } );
         ```
     
 16.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%3Freplytocom%3D1664%23feedback-editor-1664)
 17.  [Skip to note 8 content](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/?output_format=md#comment-content-2613)
 18.   [akkis](https://profiles.wordpress.org/akkis/)  [  9 years ago  ](https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_attributes/#comment-2613)
 19. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%23comment-2613)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%23comment-2613)
 20. You can add custom data attributes to image elements by adding this snippet on
     theme’s function.php:
 21.     ```php
             /**
              * Add custom data attribute to every image element
              */
             add_filter( 'wp_get_attachment_image_attributes', 'add_custom_image_data_attributes', 10, 3 );
             function add_custom_image_data_attributes( $attr, $attachment, $size ) {
     
                 // Ensure that the <img> doesn't have the data attribute already
                 if ( ! array_key_exists( 'data-custom', $attr ) ) {
                     $attr['data-custom'] = 'dummy text';
                 }
     
                 return $attr;
             }
         ```
     
 22. Also, note that I use `array_key_exists()` PHP function instead of `isset()` because`
     isset()` does not return TRUE for array keys that correspond to a NULL value, 
     while `array_key_exists()` does as of PHP Manual ([http://php.net/manual/en/function.array-key-exists.php](http://php.net/manual/en/function.array-key-exists.php)).
 23.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F%3Freplytocom%3D2613%23feedback-editor-2613)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_image_attributes%2F)
before being able to contribute a note or feedback.