get_image_tag() WordPress Function

The get_image_tag() function is used to generate an HTML img tag for an image attachment. This function can be used with any image size, but will most likely be used with the thumbnail, medium, or large image sizes. This function can also be used with the srcset attribute to provide different images for different screen sizes.

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

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.


Top ↑

Parameters

$id

(int)(Required)Attachment ID.

$alt

(string)(Required)Image description for the alt attribute.

$title

(string)(Required)Image description for the title attribute.

$align

(string)(Required)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 value: 'medium'


Top ↑

Return

(string) HTML IMG element for given image attachment


Top ↑

Source

File: wp-includes/media.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 );
}


Top ↑

Changelog

Changelog
VersionDescription
2.5.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More