wp_get_attachment_image_src( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false ): array|false
- Since
- 2.5.0
- Source
wp-includes/media.php:976
Compatibility
- WordPress
- since 2.5.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 fall back to a mime type icon. Default false.Default:
false
Return value
array|false- Array of image data, or boolean false if no image is available.
$0stringImage source URL.$1intImage width in pixels.$2intImage height in pixels.$3boolWhether the image is a resized image.
Performance profile
How much work a call to wp_get_attachment_image_src() 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
- Constant
- Instructions
- 17–68
- Plugin surface
- 2 hooks
- Called by
- 16
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 71.
Third-party callbacks on 'icon_dir', 'wp_get_attachment_image_src' run inside this call, and their cost is not bounded by anything here.
16 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below wp_get_attachment_image_src() - querycontent query
get_post()one call below wp_get_attachment_image_src()
Further down the call graph this can also reach 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 · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_get_attachment_image_src() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 17–27 | image_downsize(), apply_filters() |
| always | 25 | image_downsize(), wp_mime_type_icon(), apply_filters() |
$ext === ".svg" | 56–63 | image_downsize(), wp_mime_type_icon(), apply_filters(), wp_basename(), wp_getimagesize(), strtolower(), apply_filters() |
$ext !== ".svg" | 61–68 | image_downsize(), wp_mime_type_icon(), apply_filters(), wp_basename(), wp_getimagesize(), strtolower(), wp_getimagesize(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 71 | 17–68 | 7 | |
| 8.5 | 71 | 17–68 | 7 | |
| 8.4 | 71 | 17–68 | 7 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 74 | 17–71 | 7 | |
| 8.2 | 74 | 17–71 | 7 | 2 fewer instructions than PHP 8.1 |
| 8.1 | 76 | 17–73 | 7 | |
| 7.4 | 76 | 17–73 | 7 |
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 · 2
2 hooks fire while wp_get_attachment_image_src() runs, in this order:
- apply_filters( wp_get_attachment_image_src )filterline 1027 (+51 into the body)
Filters the attachment image source result.
Uses · 5
- image_downsize()Scales an image to fit a particular size (such as 'thumb' or 'medium').
- wp_mime_type_icon()Retrieves the icon for a MIME type or attachment.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_basename()i18n-friendly version of basename().
- wp_getimagesize()Allows PHP's getimagesize() to be debuggable when necessary.
Used by · 16
- Custom_Background::handle_upload()Handles an Image upload for the background image.
- Custom_Background::wp_set_background_image()
- Custom_Image_Header::step_2()Displays second step of custom header image page.
- WP_REST_Attachments_Controller::prepare_item_for_response()Prepares a single attachment output for response.
- edit_form_image_editor()Displays the image and editor in the post editor
- get_media_item()Retrieves HTML form for modifying the image attachment.
- get_oembed_response_data_rich()Filters the oEmbed response data to return an iframe embed code.
- twentyfifteen_post_nav_background()Adds featured image as background image to post navigation elements.
- twentytwenty_get_custom_logo()Gets the information about the logo.
- wp_get_attachment_image()Gets an HTML img element representing an image attachment.
- wp_get_attachment_image_sizes()Retrieves the value for an image attachment's 'sizes' attribute.
- wp_get_attachment_image_srcset()Retrieves the value for an image attachment's 'srcset' attribute.
Show all 16
- wp_get_attachment_image_url()Gets the URL of an image attachment.
- wp_playlist_shortcode()Builds the Playlist shortcode output.
- wp_prepare_attachment_for_js()Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.
- wp_save_image()Saves image to post, along with enqueued changes in `$_REQUEST['history']`.
Source code
function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) { // Get a thumbnail or intermediate image if there is one. $image = image_downsize( $attachment_id, $size ); if ( ! $image ) { $src = false; if ( $icon ) { $src = wp_mime_type_icon( $attachment_id, '.svg' ); if ( $src ) { /** This filter is documented in wp-includes/post.php */ $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); $src_file = $icon_dir . '/' . wp_basename( $src ); list( $width, $height ) = wp_getimagesize( $src_file ); $ext = strtolower( substr( $src_file, -4 ) ); if ( '.svg' === $ext ) { // SVG does not have true dimensions, so this assigns width and height directly. $width = 48; $height = 64; } else { list( $width, $height ) = wp_getimagesize( $src_file ); } } } if ( $src && $width && $height ) { $image = array( $src, $width, $height, false ); } } /** * Filters the attachment image source result. * * @since 4.3.0 * * @param array|false $image { * Array of image data, or boolean false if no image is available. * * @type string $0 Image source URL. * @type int $1 Image width in pixels. * @type int $2 Image height in pixels. * @type bool $3 Whether the image is a resized image. * } * @param int $attachment_id Image attachment ID. * @param string|int[] $size Requested image size. Can be any registered image size name, or * an array of width and height values in pixels (in that order). * @param bool $icon Whether the image should be treated as an icon. */ return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );}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.
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.