wppaste
WordPress

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:968
Retrieves an image to represent an attachment.

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.
  • $0string

    Image source URL.
  • $1int

    Image width in pixels.
  • $2int

    Image height in pixels.
  • $3bool

    Whether 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

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
17–68

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

Plugin surface
2 hooks

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

Called by
16

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

What it touches

  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below wp_get_attachment_image_src()
  • querycontent queryget_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.

WhenInstructionsCalls it makes
always17–27image_downsize(), apply_filters()
always25image_downsize(), wp_mime_type_icon(), apply_filters()
$ext === ".svg"56–63image_downsize(), wp_mime_type_icon(), apply_filters(), wp_basename(), wp_getimagesize(), strtolower(), apply_filters()
$ext !== ".svg"61–68image_downsize(), wp_mime_type_icon(), apply_filters(), wp_basename(), wp_getimagesize(), strtolower(), wp_getimagesize(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7117–687
8.57117–687
8.47117–6873 fewer instructions than PHP 8.3
8.37417–717
8.27417–7172 fewer instructions than PHP 8.1
8.17617–737
7.47617–737

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:

  1. apply_filters( icon_dir )filterline 979 (+11 into the body)

    Filters the icon directory path.

  2. apply_filters( wp_get_attachment_image_src )filterline 1019 (+51 into the body)

    Filters the attachment image source result.

Uses · 5

Used by · 16

Show all 16

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.

  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.

About this page

Parsed data
Generated from the wordpress-develop 6.7.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.