get_image_tag( int $id, string $alt, string $title, string $align, string|int[] $size = 'medium' ): string
- Since
- 2.5.0
- Source
wp-includes/media.php:383
Description
The 'get_image_tag_class' filter allows for changing the class name for the image without having to use regular expressions on the HTML content. The parameters are: what WordPress will use for the class, the Attachment ID, image align value, and the size the image should be.
The second filter, 'get_image_tag', has the HTML content, which can then be further manipulated by a plugin to change all attribute values and even HTML content.
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
$idint- Attachment ID.
$altstring- Image description for the alt attribute.
$titlestring- Image description for the title attribute.
$alignstring- Part of the class name for aligning the image.
$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 'medium'.Default:
'medium'
Return value
string- HTML IMG element for given image attachment.
Performance profile
How much work a call to get_image_tag() 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
- Trivial
- Scaling
- Constant
- Instructions
- 69–75
- Plugin surface
- 2 hooks
- Called by
- 1
Touches nothing outside its own arguments.
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 77.
Third-party callbacks on 'get_image_tag_class', 'get_image_tag' 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
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach query, option, cache, 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 · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost get_image_tag() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 69–70 | image_downsize(), image_hwstring(), esc_attr(), esc_attr(), apply_filters(), esc_url(), esc_attr(), apply_filters() |
| always | 74–75 | image_downsize(), image_hwstring(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), esc_url(), esc_attr(), apply_filters() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 77 | 69–75 | 2 | |
| 8.5 | 77 | 69–75 | 2 | |
| 8.4 | 77 | 69–75 | 2 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 81 | 69–79 | 2 | |
| 8.2 | 81 | 69–79 | 2 | 3 fewer instructions than PHP 8.1 |
| 8.1 | 84 | 72–82 | 2 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 85 | 72–83 | 2 |
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_image_tag() runs, in this order:
- apply_filters( get_image_tag_class )filterline 404 (+21 into the body)
Filters the value of the attachment's image tag class attribute.
- apply_filters( get_image_tag )filterline 421 (+38 into the body)
Filters the HTML content for the image tag.
Uses · 5
- image_downsize()Scales an image to fit a particular size (such as 'thumb' or 'medium').
- image_hwstring()Retrieves width and height attributes using given width and height values.
- esc_attr()Escaping for HTML attributes.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- esc_url()Checks and cleans a URL.
Used by · 1
- get_image_send_to_editor()Retrieves the image HTML to send to the editor.
Source code
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { list( $img_src, $width, $height ) = image_downsize( $id, $size ); $hwstring = image_hwstring( $width, $height ); $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; $size_class = is_array( $size ) ? implode( 'x', $size ) : $size; $class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size_class ) . ' wp-image-' . $id; /** * Filters the value of the attachment's image tag class attribute. * * @since 2.6.0 * * @param string $class CSS class name or space-separated list of classes. * @param int $id Attachment ID. * @param string $align Part of the class name for aligning the image. * @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). */ $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); $html = '<img src="' . esc_url( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; /** * Filters the HTML content for the image tag. * * @since 2.6.0 * * @param string $html HTML content for the image. * @param int $id Attachment ID. * @param string $alt Image description for the alt attribute. * @param string $title Image description for the title attribute. * @param string $align Part of the class name for aligning the image. * @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). */ return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );}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.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.