wppaste
WordPress

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:400
Gets an img tag for an image attachment, scaling it down if requested.

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

Touches nothing outside its own arguments.

Scaling
Constant

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

Instructions
69–75

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

Plugin surface
2 hooks

Third-party callbacks on 'get_image_tag_class', 'get_image_tag' 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

  • hookthird-party callbacksapply_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.

WhenInstructionsCalls it makes
always69–70image_downsize(), image_hwstring(), esc_attr(), esc_attr(), apply_filters(), esc_url(), esc_attr(), apply_filters()
always74–75image_downsize(), image_hwstring(), esc_attr(), esc_attr(), esc_attr(), apply_filters(), esc_url(), esc_attr(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7769–752
8.57769–752
8.47769–7524 fewer instructions than PHP 8.3
8.38169–792
8.28169–7923 fewer instructions than PHP 8.1
8.18472–8221 fewer instruction than PHP 7.4
7.48572–832

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:

  1. apply_filters( get_image_tag_class )filterline 421 (+21 into the body)

    Filters the value of the attachment's image tag class attribute.

  2. apply_filters( get_image_tag )filterline 438 (+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

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.

  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 7.1.0 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.