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

Filters the list of attachment image attributes.

Parameters

$attrstring[]
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 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.
    Defaults to 'lazy', depending on wp_lazy_loading_enabled() .
  • 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.
$attachmentWP_Post
Image attachment post.
$sizestring|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

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

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 6 content

    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.

    /**
     * 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 );
  2. Skip to note 8 content

    You can add custom data attributes to image elements by adding this snippet on theme’s function.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;
        }

    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).

You must log in before being able to contribute a note or feedback.