Filter hook 'wp_img_tag_add_srcset_and_sizes_attr'

in WP Core File wp-includes/media.php at line 2311

View Source

wp_img_tag_add_srcset_and_sizes_attr

Filter Hook
Description
Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`. Returning anything else than `true` will not add the attributes.

Hook Information

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

Hook Parameters

Type Name Description
bool $value The filtered value, defaults to `true`.
string $image The HTML `img` tag where the attribute should be added.
string $context Additional context about how the function was called or where the img tag is.
int $attachment_id The image attachment ID.

Usage Examples

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

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

Source Code Context

wp-includes/media.php:2311 - How this hook is used in WordPress core
<?php
2306  	 * @param bool   $value         The filtered value, defaults to `true`.
2307  	 * @param string $image         The HTML `img` tag where the attribute should be added.
2308  	 * @param string $context       Additional context about how the function was called or where the img tag is.
2309  	 * @param int    $attachment_id The image attachment ID.
2310  	 */
2311  	$add = apply_filters( 'wp_img_tag_add_srcset_and_sizes_attr', true, $image, $context, $attachment_id );
2312  
2313  	if ( true === $add ) {
2314  		$image_meta = wp_get_attachment_metadata( $attachment_id );
2315  		return wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id );
2316  	}

PHP Documentation

<?php
/**
	 * Filters whether to add the `srcset` and `sizes` HTML attributes to the img tag. Default `true`.
	 *
	 * Returning anything else than `true` will not add the attributes.
	 *
	 * @since 5.5.0
	 *
	 * @param bool   $value         The filtered value, defaults to `true`.
	 * @param string $image         The HTML `img` tag where the attribute should be added.
	 * @param string $context       Additional context about how the function was called or where the img tag is.
	 * @param int    $attachment_id The image attachment ID.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 4
  • File: wp-includes/media.php
Related Hooks

Related hooks will be displayed here in future updates.