get_image_tag()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
get_image_tag( int $id, string $alt, string $title, string $align, string|int[] $size = 'medium' ): string
Описание
Фильтр 'get_image_tag_class' позволяет изменить имя класса изображения без использования регулярных выражений над HTML-содержимым. Его параметры: имя класса, которое использует WordPress, идентификатор вложения, значение выравнивания изображения и требуемый размер изображения.
Второй фильтр, 'get_image_tag', получает HTML-содержимое, которое затем плагин может дополнительно изменить, поменяв все значения атрибутов и даже само HTML-содержимое.
Оригинал (английский)
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.
Параметры
$id
int
обязательный
$alt
string
обязательный
$title
string
обязательный
$align
string
обязательный
$size
string|int[]
необязательный
= 'medium'
Возвращаемое значение
string
Исходный код
wp-includes/media.php:388
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 );
}
История изменений
| Версия | Описание |
|---|---|
| 2.5.0 | Introduced. |

