wp_img_tag_add_decoding_attr()WP 6.1.0

Deprecated from version 6.4.0. It is no longer supported and can be removed in future releases. Use wp_img_tag_add_loading_optimization_attrs() instead.

Adds decoding attribute to an img HTML tag.

The decoding attribute allows developers to indicate whether the browser can decode the image off the main thread (async), on the main thread (sync) or as determined by the browser (auto).

By default WordPress adds decoding="async" to images but developers can use the wp_img_tag_add_decoding_attr filter to modify this to remove the attribute or set it to another accepted value.

1 time — 0.0000272 sec (very fast) | 50000 times — 0.10 sec (speed of light) | PHP 7.4.33, WP 6.1.1
Hooks from the function

Return

String. Converted img tag with decoding attribute added.

Usage

wp_img_tag_add_decoding_attr( $image, $context );
$image(string) (required)
The HTML img tag where the attribute should be added.
$context(string) (required)
Additional context to pass to the filters.

Notes

Changelog

Since 6.1.0 Introduced.
Deprecated since 6.4.0 Use wp_img_tag_add_loading_optimization_attrs() instead.

wp_img_tag_add_decoding_attr() code WP 6.4.3

function wp_img_tag_add_decoding_attr( $image, $context ) {
	_deprecated_function( __FUNCTION__, '6.4.0', 'wp_img_tag_add_loading_optimization_attrs()' );

	/*
	 * Only apply the decoding attribute to images that have a src attribute that
	 * starts with a double quote, ensuring escaped JSON is also excluded.
	 */
	if ( ! str_contains( $image, ' src="' ) ) {
		return $image;
	}

	/** This action is documented in wp-includes/media.php */
	$value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context );

	if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) {
		$image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image );
	}

	return $image;
}