WordPress DocsDocs (opens in a new tab)

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

  • $attr array — Image HTML attributes.
  • $attachment WP_Post — Attachment post object.
  • $size string|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

Group wizard

One session: this hook, then each related_hooks entry. Walk it and tell us what to change.

wp_get_attachment_image_attributes → upload_mimes

functions.php — wp_get_attachment_image_attributes
functions.php
PHP wp_get_attachment_image_attributes · Hook 1 of 2 · Step 1 of 6

Practice the wp_get_attachment_image_attributes WordPress filter hook in our interactive sandbox — a VS Code-style editor with a step-by-step wp_get_attachment_image_attributes tutorial, add_filter exercises, and instant feedback. No local WordPress install required.