wppaste
WordPress

wp_get_attachment_image( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false, string|array $attr = '' ): string

Since
2.5.0, 4.4.0, 5.5.0, 6.1.0
Source
wp-includes/media.php:1068
Gets an HTML img element representing an image attachment.

Description

While $size will accept an array, it is better to register a size with add_image_size() so that a cropped version is generated. It's much more efficient than having to find the closest-sized image and then having the browser scale down the image.

Compatibility

WordPress
since 6.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attachment_idint
Image attachment ID.
$sizestring|int[]optional
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'thumbnail'.Default: 'thumbnail'
$iconbooloptional
Whether the image should be treated as an icon. Default false.Default: false
$attrstring|arrayoptional
Attributes for the image markup.Default: ''
  • $srcstring

    Image attachment URL.
  • $classstring

    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.
  • $altstring

    Image description for the alt attribute.
  • $srcsetstring

    The 'srcset' attribute value.
  • $sizesstring

    The 'sizes' attribute value.
  • $loadingstring|falsedefault: determined by {@see wp_get_loading_optimization_attributes()}

    The 'loading' attribute value. Passing a value of false will result in the attribute being omitted for the image.
  • $decodingstring

    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.
  • $fetchprioritystringdefault: determined by {@see wp_get_loading_optimization_attributes()}

    The 'fetchpriority' attribute value, whether high, low, or auto.

Return value

string
HTML img element or empty string on failure.

Performance profile

How much work a call to wp_get_attachment_image() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
21–203

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 226.

Plugin surface
4 hooks

Third-party callbacks on 'wp_get_attachment_image_context', 'wp_img_tag_add_auto_sizes', 'wp_get_attachment_image_attributes' run inside this call, and their cost is not bounded by anything here.

Called by
12

12 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • querycontent queryget_post()called directly
  • hookthird-party callbacksapply_filters()called directly

Further down the call graph this can also reach cache, option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 19 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_attachment_image() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always21wp_get_attachment_image_src(), apply_filters()
!is_array($size_class) && !isset($attr)125–143wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), apply_filters(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && !isset($attr) && empty($attr) && !is_array($image_meta)131–149wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), apply_filters(), apply_filters(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class)134–149wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), apply_filters(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class)138–153wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && !is_array($image_meta)140–155wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), apply_filters(), apply_filters(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class)143–155wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), apply_filters(), absint(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && !is_array($image_meta)144–159wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class)147–159wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && !is_array($image_meta)149–161wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), apply_filters(), apply_filters(), absint(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && !is_array($image_meta)153–165wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && !isset($attr) && empty($attr) && is_array($image_meta)155–181wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), absint(), absint(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), apply_filters(), apply_filters(), array_map(), image_hwstring(), rtrim(), apply_filters()
7 further outcomes, up to 203 instructions
!is_array($size_class)156–165wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), absint(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && !is_array($image_meta)162–171wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), absint(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && is_array($image_meta)164–187wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), absint(), absint(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), apply_filters(), apply_filters(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && is_array($image_meta)168–191wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), absint(), absint(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && is_array($image_meta)173–193wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), absint(), absint(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), apply_filters(), apply_filters(), absint(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && is_array($image_meta)177–197wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), absint(), absint(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()
!is_array($size_class) && empty($attr) && is_array($image_meta)186–203wp_get_attachment_image_src(), get_post(), get_post_meta(), strip_tags(), apply_filters(), wp_parse_args(), wp_get_loading_optimization_attributes(), array_merge(), wp_get_attachment_metadata(), absint(), absint(), wp_calculate_image_srcset(), wp_calculate_image_sizes(), apply_filters(), wp_sizes_attribute_includes_valid_auto(), apply_filters(), absint(), absint(), array_map(), image_hwstring(), rtrim(), apply_filters()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev22621–20329
8.522621–20329
8.422621–2032913 fewer instructions than PHP 8.3
8.323921–20929
8.223921–209293 fewer instructions than PHP 8.1
8.124221–21229
7.424221–21229

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 4

4 hooks fire while wp_get_attachment_image() runs, in this order:

  1. apply_filters( wp_get_attachment_image_context )filterline 1095 (+27 into the body)

    Filters the context in which wp_get_attachment_image() is used.

  2. apply_filters( wp_img_tag_add_auto_sizes )filterline 1156 (+88 into the body)

    Filters whether auto-sizes for lazy loaded images is enabled.

  3. apply_filters( wp_get_attachment_image_attributes )filterline 1181 (+113 into the body)

    Filters the list of attachment image attributes.

  4. apply_filters( wp_get_attachment_image )filterline 1215 (+147 into the body)

    Filters the HTML img element representing an image attachment.

Uses · 12

Used by · 12

Source code

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;

Changelog

Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.1.0
The $decoding attribute was added.from the docblock
5.5.0
The $loading attribute was added.from the docblock
4.4.0
The $srcset and $sizes attributes were added.from the docblock
2.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/media.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.