# get_header_image_tag()

URL: https://chugunov.pro/api-wordpress/functions/get_header_image_tag/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 4.4.0.

## Сигнатура

```php
get_header_image_tag( array $attr = array() ): string
```

## Описание

Создаёт разметку тега изображения для произвольного изображения шапки.

## Параметры

- `$attr` `array` — необязательный, по умолчанию `array()`. Дополнительные атрибуты для тега изображения. Могут использоваться для переопределения атрибутов по умолчанию.

## Возвращаемое значение

`string`

## Исходный код

Файл: `wp-includes/theme.php:1267`

```php
function get_header_image_tag( $attr = array() ) {
	$header      = get_custom_header();
	$header->url = get_header_image();

	if ( ! $header->url ) {
		return '';
	}

	$width  = absint( $header->width );
	$height = absint( $header->height );
	$alt    = '';

	// Use alternative text assigned to the image, if available. Otherwise, leave it empty.
	if ( ! empty( $header->attachment_id ) ) {
		$image_alt = get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true );

		if ( is_string( $image_alt ) ) {
			$alt = $image_alt;
		}
	}

	$attr = wp_parse_args(
		$attr,
		array(
			'src'    => $header->url,
			'width'  => $width,
			'height' => $height,
			'alt'    => $alt,
		)
	);

	// Generate 'srcset' and 'sizes' if not already present.
	if ( empty( $attr['srcset'] ) && ! empty( $header->attachment_id ) ) {
		$image_meta = get_post_meta( $header->attachment_id, '_wp_attachment_metadata', true );
		$size_array = array( $width, $height );

		if ( is_array( $image_meta ) ) {
			$srcset = wp_calculate_image_srcset( $size_array, $header->url, $image_meta, $header->attachment_id );

			if ( ! empty( $attr['sizes'] ) ) {
				$sizes = $attr['sizes'];
			} else {
				$sizes = wp_calculate_image_sizes( $size_array, $header->url, $image_meta, $header->attachment_id );
			}

			if ( $srcset && $sizes ) {
				$attr['srcset'] = $srcset;
				$attr['sizes']  = $sizes;
			}
		}
	}

	$attr = array_merge(
		$attr,
		wp_get_loading_optimization_attributes( 'img', $attr, 'get_header_image_tag' )
	);

	/*
	 * If the default value of `lazy` for the `loading` attribute is overridden
	 * to omit the attribute for this image, ensure it is not included.
	 */
	if ( isset( $attr['loading'] ) && ! $attr['loading'] ) {
		unset( $attr['loading'] );
	}

	// If the `fetchpriority` attribute is overridden and set to false or an empty string.
	if ( isset( $attr['fetchpriority'] ) && ! $attr['fetchpriority'] ) {
		unset( $attr['fetchpriority'] );
	}

	// If the `decoding` attribute is overridden and set to false or an empty string.
	if ( isset( $attr['decoding'] ) && ! $attr['decoding'] ) {
		unset( $attr['decoding'] );
	}

	/**
	 * Filters the list of header image attributes.
	 *
	 * @since 5.9.0
	 *
	 * @param array  $attr   Array of the attributes for the image tag.
	 * @param object $header The custom header object returned by 'get_custom_header()'.
	 */
	$attr = apply_filters( 'get_header_image_tag_attributes', $attr, $header );

	$attr = array_map( 'esc_attr', $attr );
	$html = '<img';

	foreach ( $attr as $name => $value ) {
		$html .= ' ' . $name . '="' . $value . '"';
	}

	$html .= ' />';

	/**
	 * Filters the markup of header images.
	 *
	 * @since 4.4.0
	 *
	 * @param string $html   The HTML image tag markup being filtered.
	 * @param object $header The custom header object returned by 'get_custom_header()'.
	 * @param array  $attr   Array of the attributes for the image tag.
	 */
	return apply_filters( 'get_header_image_tag', $html, $header, $attr );
}
```

## История изменений

- 4.4.0 — Introduced.

## Связанные

Использует: [`wp_get_loading_optimization_attributes`](https://chugunov.pro/api-wordpress/functions/wp_get_loading_optimization_attributes/), [`wp_calculate_image_srcset`](https://chugunov.pro/api-wordpress/functions/wp_calculate_image_srcset/), [`wp_calculate_image_sizes`](https://chugunov.pro/api-wordpress/functions/wp_calculate_image_sizes/), [`get_custom_header`](https://chugunov.pro/api-wordpress/functions/get_custom_header/), [`get_header_image`](https://chugunov.pro/api-wordpress/functions/get_header_image/), [`absint`](https://chugunov.pro/api-wordpress/functions/absint/), [`wp_parse_args`](https://chugunov.pro/api-wordpress/functions/wp_parse_args/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), [`get_post_meta`](https://chugunov.pro/api-wordpress/functions/get_post_meta/).
Используется в: [`get_custom_header_markup`](https://chugunov.pro/api-wordpress/functions/get_custom_header_markup/), [`the_header_image_tag`](https://chugunov.pro/api-wordpress/functions/the_header_image_tag/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/get_header_image_tag/
