wp-includes/media.php:1169Gets an HTML img element representing an image attachment.
$attachment_idint$sizestring|int[]optional'thumbnail'$iconbooloptionalfalse$attrstring|arrayoptional''$srcstring
$classstring
$altstring
$srcsetstring
$sizesstring
$loadingstring|falsedefault: determined by {@see wp_get_loading_optimization_attributes()}
$decodingstring
$fetchprioritystringdefault: determined by {@see wp_get_loading_optimization_attributes()}
string4 hooks fire while wp_get_attachment_image() runs, in this order:
Filters the context in which wp_get_attachment_image() is used.
Filters whether auto-sizes for lazy loaded images is enabled.
Filters the list of attachment image attributes.
Filters the HTML img element representing an image attachment.
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) { $html = ''; $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); if ( $image ) { list( $src, $width, $height ) = $image; $attachment = get_post( $attachment_id ); $size_class = $size; if ( is_array( $size_class ) ) { $size_class = implode( 'x', $size_class ); } $default_attr = array( 'src' => $src, 'class' => "attachment-$size_class size-$size_class", 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ), ); /** * Filters the context in which wp_get_attachment_image() is used. * * @since 6.3.0 * * @param string $context The context. Default 'wp_get_attachment_image'. */ $context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' ); $attr = wp_parse_args( $attr, $default_attr ); // Ensure that the `$width` doesn't overwrite an already valid user-provided width. if ( ! isset( $attr['width'] ) || ! is_numeric( $attr['width'] ) ) { $attr['width'] = $width; } // Ensure that the `$height` doesn't overwrite an already valid user-provided height. if ( ! isset( $attr['height'] ) || ! is_numeric( $attr['height'] ) ) { $attr['height'] = $height; } $loading_optimization_attr = wp_get_loading_optimization_attributes( 'img', $attr, $context ); // Add loading optimization attributes if not available. $attr = array_merge( $attr, $loading_optimization_attr ); // Omit the `decoding` attribute if the value is invalid according to the spec. if ( empty( $attr['decoding'] ) || ! in_array( $attr['decoding'], array( 'async', 'sync', 'auto' ), true ) ) { unset( $attr['decoding'] ); } /* * If the default value of `lazy` for the `loading` attribute is overridden * to omit the attribute for this image, ensure it is not included. */ if ( isset( $attr['loading'] ) && ! $attr['loading'] ) { unset( $attr['loading'] ); } // If the `fetchpriority` attribute is overridden and set to false or an empty string. if ( isset( $attr['fetchpriority'] ) && ! $attr['fetchpriority'] ) { unset( $attr['fetchpriority'] ); } // Generate 'srcset' and 'sizes' if not already present. if ( empty( $attr['srcset'] ) ) { $image_meta = wp_get_attachment_metadata( $attachment_id ); if ( is_array( $image_meta ) ) { $size_array = array( absint( $width ), absint( $height ) ); $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { $attr['srcset'] = $srcset;Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.