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:1169
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:
''$srcstringImage attachment URL.$classstringCSS class name or space-separated list of classes. Defaultattachment-$size_class size-$size_class, where$size_classis the image size being requested.$altstringImage description for the alt attribute.$srcsetstringThe 'srcset' attribute value.$sizesstringThe '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.$decodingstringThe '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, whetherhigh,low, orauto.
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
- Scaling
- Scales with input
- Instructions
- 21–203
- Plugin surface
- 4 hooks
- Called by
- 13
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 226.
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.
13 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 21 | wp_get_attachment_image_src(), apply_filters() |
!is_array($size_class) && !isset($attr) | 125–143 | wp_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–149 | wp_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–149 | wp_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–153 | wp_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–155 | wp_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–155 | wp_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–159 | wp_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–159 | wp_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–161 | wp_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–165 | wp_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–181 | wp_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–165 | wp_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–171 | wp_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–187 | wp_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–191 | wp_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–193 | wp_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–197 | wp_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–203 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 226 | 21–203 | 29 | |
| 8.5 | 226 | 21–203 | 29 | |
| 8.4 | 226 | 21–203 | 29 | 13 fewer instructions than PHP 8.3 |
| 8.3 | 239 | 21–209 | 29 | |
| 8.2 | 239 | 21–209 | 29 | 3 fewer instructions than PHP 8.1 |
| 8.1 | 242 | 21–212 | 29 | |
| 7.4 | 242 | 21–212 | 29 |
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:
- apply_filters( wp_get_attachment_image_context )filterline 1196 (+27 into the body)
Filters the context in which wp_get_attachment_image() is used.
- apply_filters( wp_img_tag_add_auto_sizes )filterline 1257 (+88 into the body)
Filters whether auto-sizes for lazy loaded images is enabled.
- apply_filters( wp_get_attachment_image_attributes )filterline 1282 (+113 into the body)
Filters the list of attachment image attributes.
- apply_filters( wp_get_attachment_image )filterline 1316 (+147 into the body)
Filters the HTML img element representing an image attachment.
Uses · 12
- wp_get_attachment_image_src()Retrieves an image to represent an attachment.
- get_post()Retrieves post data given a post ID or post object.
- get_post_meta()Retrieves a post meta field for the given post ID.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_parse_args()Merges user defined arguments into defaults array.
- wp_get_loading_optimization_attributes()Gets loading optimization attributes.
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- absint()Converts a value to non-negative integer.
- wp_calculate_image_srcset()A helper function to calculate the image sources to include in a 'srcset' attribute.
- wp_calculate_image_sizes()Creates a 'sizes' attribute value for an image.
- wp_sizes_attribute_includes_valid_auto()Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list.
- image_hwstring()Retrieves width and height attributes using given width and height values.
Used by · 13
- Twenty_Fourteen_Ephemera_Widget::widget()Outputs the HTML for this widget.
- WP_Comments_List_Table::column_response()Outputs the response column.
- WP_Media_List_Table::column_title()Handles the title column output.
- WP_Widget_Media_Image::render_media()Render the media on the frontend.
- _wp_post_thumbnail_html()Returns HTML for the post thumbnail meta box.
- block_core_gallery_render_dynamic_image()Renders a single `core/image` block for a Gallery block running in dynamic mode, applying the gallery-wide settings that affect how an image renders.
- gallery_shortcode()Builds the Gallery shortcode output.
- get_custom_logo()Returns a custom logo, linked to home unless the theme supports removing the link on the home page.
- get_the_post_thumbnail()Retrieves the post thumbnail.
- set_post_thumbnail()Sets the post thumbnail (featured image) for the given post.
- twentyfourteen_the_attached_image()Prints the attached image with a link to the next attached image.
- twentythirteen_the_attached_image()Prints the attached image with a link to the next attached image.
Show all 13
- wp_get_attachment_link()Retrieves an attachment page link using an image or icon, if possible.
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.
Signature, return type and hooks compared across 5 parsed releases.
$decoding attribute was added.from the docblock$loading attribute was added.from the docblock$srcset and $sizes attributes were added.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 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.