# wp_image_add_srcset_and_sizes()

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

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

## Сигнатура

```php
wp_image_add_srcset_and_sizes( string $image, array $image_meta, int $attachment_id ): string
```

## Описание

См. alsowp_calculate_image_srcset()

wp_calculate_image_sizes()

## Параметры

- `$image` `string` — обязательный. HTML-элемент 'img', подлежащий фильтрации.
- `$image_meta` `array` — обязательный. Метаданные изображения в том виде, в каком их возвращает 'wp_get_attachment_metadata() '.
- `$attachment_id` `int` — обязательный. Идентификатор вложения-изображения.

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

`string` — 'img' 'srcset' 'sizes'

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

Файл: `wp-includes/media.php:1773`

```php
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
	// Ensure the image meta exists.
	if ( empty( $image_meta['sizes'] ) ) {
		return $image;
	}

	$image_src         = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
	list( $image_src ) = explode( '?', $image_src );

	// Return early if we couldn't get the image source.
	if ( ! $image_src ) {
		return $image;
	}

	// Bail early if an image has been inserted and later edited.
	if ( isset( $image_meta['file'] ) && preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash )
		&& ! str_contains( wp_basename( $image_src ), $img_edit_hash[0] )
	) {
		return $image;
	}

	$width  = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0;
	$height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;

	if ( $width && $height ) {
		$size_array = array( $width, $height );
	} else {
		$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );
		if ( ! $size_array ) {
			return $image;
		}
	}

	$srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );

	if ( $srcset ) {
		// Check if there is already a 'sizes' attribute.
		$sizes = strpos( $image, ' sizes=' );

		if ( ! $sizes ) {
			$sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
		}
	}

	if ( $srcset && $sizes ) {
		// Format the 'srcset' and 'sizes' string and escape attributes.
		$attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) );

		if ( is_string( $sizes ) ) {
			$attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) );
		}

		// Add the srcset and sizes attributes to the image markup.
		return preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image );
	}

	return $image;
}
```

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

- 4.4.0 — Introduced.

## Связанные

Использует: [`wp_image_src_get_dimensions`](https://chugunov.pro/api-wordpress/functions/wp_image_src_get_dimensions/), [`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/), [`wp_basename`](https://chugunov.pro/api-wordpress/functions/wp_basename/), [`esc_attr`](https://chugunov.pro/api-wordpress/functions/esc_attr/).
Используется в: [`wp_img_tag_add_srcset_and_sizes_attr`](https://chugunov.pro/api-wordpress/functions/wp_img_tag_add_srcset_and_sizes_attr/).

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