Filter hook 'wp_img_tag_add_decoding_attr'

in WP Core File wp-includes/deprecated.php at line 6046

View Source

wp_img_tag_add_decoding_attr

Filter Hook
Description
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 {@see 'wp_img_tag_add_decoding_attr'} filter to modify this to remove the attribute or set it to another accepted value. / 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.

Hook Information

File Location wp-includes/deprecated.php View on GitHub
Hook Type Filter
Line Number 6046

Hook Parameters

Type Name Description
string $image The HTML `img` tag where the attribute should be added.
string $context Additional context to pass to the filters.

Usage Examples

Basic Usage
<?php
// Hook into wp_img_tag_add_decoding_attr
add_filter('wp_img_tag_add_decoding_attr', 'my_custom_filter', 10, 2);

function my_custom_filter($image, $context) {
    // Your custom filtering logic here
    return $image;
}

Source Code Context

wp-includes/deprecated.php:6046 - How this hook is used in WordPress core
<?php
6041  	if ( ! str_contains( $image, ' src="' ) ) {
6042  		return $image;
6043  	}
6044  
6045  	/** This action is documented in wp-includes/media.php */
6046  	$value = apply_filters( 'wp_img_tag_add_decoding_attr', 'async', $image, $context );
6047  
6048  	if ( in_array( $value, array( 'async', 'sync', 'auto' ), true ) ) {
6049  		$image = str_replace( '<img ', '<img decoding="' . esc_attr( $value ) . '" ', $image );
6050  	}
6051  

PHP Documentation

<?php
/**
 * 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 {@see 'wp_img_tag_add_decoding_attr'} filter to modify this
 * to remove the attribute or set it to another accepted value.
 *
 * @since 6.1.0
 * @deprecated 6.4.0 Use wp_img_tag_add_loading_optimization_attrs() instead.
 * @see wp_img_tag_add_loading_optimization_attrs()
 *
 * @param string $image   The HTML `img` tag where the attribute should be added.
 * @param string $context Additional context to pass to the filters.
 * @return string Converted `img` tag with `decoding` attribute added.
 */
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.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 2
  • File: wp-includes/deprecated.php
Related Hooks

Related hooks will be displayed here in future updates.