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
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
- Scaling
- Constant
- Instructions
- 16–93
- Plugin surface
- 2 hooks
- Called by
- 1
Reads or writes the filesystem via file_exists().
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 116.
Third-party callbacks on 'attachment_max_dims', 'attachment_icon' run inside this call, and their cost is not bounded by anything here.
1 place 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 - filesystemfilesystem access
file_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 16 | _deprecated_function(), get_post() |
| always | 24 | _deprecated_function(), get_post(), get_attachment_icon_src() |
!$max_dims | 58 | _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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 116 | 16–93 | 7 | |
| 8.5 | 116 | 16–93 | 7 | |
| 8.4 | 116 | 16–93 | 7 | |
| 8.3 | 116 | 16–93 | 7 | |
| 8.2 | 116 | 16–93 | 7 | 1 fewer instruction than PHP 8.1 |
| 8.1 | 117 | 16–94 | 7 | |
| 7.4 | 117 | 16–94 | 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 get_attachment_icon() runs, in this order:
- apply_filters( attachment_max_dims )filterline 1954 (+12 into the body)
- apply_filters( attachment_icon )filterline 1983 (+41 into the body)
Uses · 6
- _deprecated_function()Marks a function as deprecated and inform when it has been used.
- get_post()Retrieves post data given a post ID or post object.
- get_attachment_icon_src()Retrieve icon URL and Path.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_getimagesize()Allows PHP's getimagesize() to be debuggable when necessary.
- esc_attr()Escaping for HTML attributes.
Used by · 1
- get_attachment_innerHTML()Retrieve HTML content of image element.
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.
Signature, return type and hooks compared across 5 parsed releases.
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.