Title: get_image_tag
Published: April 25, 2014
Last modified: February 24, 2026

---

# get_image_tag( int $id, string $alt, string $title, string $align, string|int[] $size ): string

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#wp--skip-link--target)

Gets an img tag for an image attachment, scaling it down if requested.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#description)󠁿

The [‘get_image_tag_class’](https://developer.wordpress.org/reference/hooks/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’](https://developer.wordpress.org/reference/hooks/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.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#parameters)󠁿

 `$id`intrequired

Attachment ID.

`$alt`stringrequired

Image description for the alt attribute.

`$title`stringrequired

Image description for the title attribute.

`$align`stringrequired

Part of the class name for aligning the image.

`$size`string|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'`.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#return)󠁿

 string HTML IMG element for given image attachment.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#source)󠁿

    ```php
    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 );
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/media.php#L388)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/media.php#L388-L427)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#hooks)󠁿

 [apply_filters( ‘get_image_tag’, string $html, int $id, string $alt, string $title, string $align, string|int[] $size )](https://developer.wordpress.org/reference/hooks/get_image_tag/)

Filters the HTML content for the image tag.

 [apply_filters( ‘get_image_tag_class’, string $class, int $id, string $align, string|int[] $size )](https://developer.wordpress.org/reference/hooks/get_image_tag_class/)

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#related)󠁿

| Uses | Description | 
| [image_downsize()](https://developer.wordpress.org/reference/functions/image_downsize/)`wp-includes/media.php` |

Scales an image to fit a particular size (such as ‘thumb’ or ‘medium’).

  | 
| [image_hwstring()](https://developer.wordpress.org/reference/functions/image_hwstring/)`wp-includes/media.php` |

Retrieves width and height attributes using given width and height values.

  | 
| [esc_attr()](https://developer.wordpress.org/reference/functions/esc_attr/)`wp-includes/formatting.php` |

Escaping for HTML attributes.

  | 
| [esc_url()](https://developer.wordpress.org/reference/functions/esc_url/)`wp-includes/formatting.php` |

Checks and cleans a URL.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 3 more](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#)

| Used by | Description | 
| [get_image_send_to_editor()](https://developer.wordpress.org/reference/functions/get_image_send_to_editor/)`wp-admin/includes/media.php` |

Retrieves the image HTML to send to the editor.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_image_tag/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.5.0](https://developer.wordpress.org/reference/since/2.5.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_image_tag%2F)
before being able to contribute a note or feedback.