wp_img_tag_add_srcset_and_sizes_attr( string $image, string $context, int $attachment_id ): string

In this article

Adds srcset and sizes attributes to an existing img HTML tag.

Parameters

$imagestringrequired
The HTML img tag where the attribute should be added.
$contextstringrequired
Additional context to pass to the filters.
$attachment_idintrequired
Image attachment ID.

Return

string Converted 'img' element with 'loading' attribute added.

Source

		$image = str_replace( '<img', '<img decoding="' . esc_attr( $optimization_attrs['decoding'] ) . '"', $image );
	}
}

// Images should have dimension attributes for the 'loading' and 'fetchpriority' attributes to be added.
if ( ! str_contains( $image, ' width="' ) || ! str_contains( $image, ' height="' ) ) {
	return $image;
}

// Retained for backward compatibility.
$loading_attrs_enabled = wp_lazy_loading_enabled( 'img', $context );

if ( empty( $loading_val ) && $loading_attrs_enabled ) {
	/**
	 * Filters the `loading` attribute value to add to an image. Default `lazy`.
	 *
	 * Returning `false` or an empty string will not add the attribute.
	 * Returning `true` will add the default value.
	 *
	 * @since 5.5.0
	 *
	 * @param string|bool $value   The `loading` attribute value. Returning a falsey value will result in

Changelog

VersionDescription
5.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.