wppaste
WordPress

get_attachment_icon( int $id = 0, bool $fullsize = false, array $max_dims = false ): string|false

Since
2.0.0
Source
wp-includes/deprecated.php:1942
Retrieve HTML content of icon attachment image element.

Compatibility

Deprecated in WordPress 2.5.0. Use wp_get_attachment_image()

WordPress
since 2.0.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

$idintoptional
Post ID.Default: 0
$fullsizebooloptional
Whether to have full size image. Default false.Default: false
$max_dimsarrayoptional
Dimensions of image.Default: false

Return value

string|false
HTML content.

Performance profile

How much work a call to get_attachment_icon() 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

Reads or writes the filesystem via file_exists().

Scaling
Constant

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

Instructions
16–93

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

Plugin surface
2 hooks

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

Called by
1

1 place 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
  • filesystemfilesystem accessfile_exists()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 · 5 distinct outcomes

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

WhenInstructionsCalls it makes
always16_deprecated_function(), get_post()
always24_deprecated_function(), get_post(), get_attachment_icon_src()
!$max_dims58_deprecated_function(), get_post(), get_attachment_icon_src(), apply_filters(), esc_attr(), apply_filters()
$max_dims && !file_exists()62_deprecated_function(), get_post(), get_attachment_icon_src(), apply_filters(), file_exists(), esc_attr(), apply_filters()
$max_dims && file_exists()81–93_deprecated_function(), get_post(), get_attachment_icon_src(), apply_filters(), file_exists(), wp_getimagesize(), esc_attr(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11616–937
8.511616–937
8.411616–937
8.311616–937
8.211616–9371 fewer instruction than PHP 8.1
8.111716–947
7.411716–947

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 get_attachment_icon() runs, in this order:

  1. apply_filters( attachment_max_dims )filterline 1954 (+12 into the body)
  2. apply_filters( attachment_icon )filterline 1983 (+41 into the body)

Uses · 6

Used by · 1

Source code

function get_attachment_icon( $id = 0, $fullsize = false, $max_dims = false ) {	_deprecated_function( __FUNCTION__, '2.5.0', 'wp_get_attachment_image()' );	$id = (int) $id;	if ( !$post = get_post($id) )		return false; 	if ( !$src = get_attachment_icon_src( $post->ID, $fullsize ) )		return false; 	list($src, $src_file) = $src; 	// Do we need to constrain the image?	if ( ($max_dims = apply_filters('attachment_max_dims', $max_dims)) && file_exists($src_file) ) { 		$imagesize = wp_getimagesize($src_file); 		if (($imagesize[0] > $max_dims[0]) || $imagesize[1] > $max_dims[1] ) {			$actual_aspect = $imagesize[0] / $imagesize[1];			$desired_aspect = $max_dims[0] / $max_dims[1]; 			if ( $actual_aspect >= $desired_aspect ) {				$height = $actual_aspect * $max_dims[0];				$constraint = "width='{$max_dims[0]}' ";				$post->iconsize = array($max_dims[0], $height);			} else {				$width = $max_dims[1] / $actual_aspect;				$constraint = "height='{$max_dims[1]}' ";				$post->iconsize = array($width, $max_dims[1]);			}		} else {			$post->iconsize = array($imagesize[0], $imagesize[1]);			$constraint = '';		}	} else {		$constraint = '';	} 	$post_title = esc_attr($post->post_title); 	$icon = "<img src='$src' title='$post_title' alt='$post_title' $constraint/>"; 	return apply_filters( 'attachment_icon', $icon, $post->ID );}

Changelog

Introduced in 2.0.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.

2.5.0
Deprecated. Use wp_get_attachment_image()from the docblock
2.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/deprecated.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.