Title: wp_img_tag_add_auto_sizes
Published: February 24, 2026
Last modified: August 20, 2026

---

# wp_img_tag_add_auto_sizes( string $image ): string

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#wp--skip-link--target)

Adds ‘auto’ to the sizes attribute to the image, if the image is lazy loaded and
does not already include it.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#parameters)󠁿

 `$image`stringrequired

The image tag markup being filtered.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#return)󠁿

 string The filtered image tag markup.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#source)󠁿

    ```php
    function wp_img_tag_add_auto_sizes( string $image ): string {
    	/**
    	 * Filters whether auto-sizes for lazy loaded images is enabled.
    	 *
    	 * @since 6.7.1
    	 *
    	 * @param boolean $enabled Whether auto-sizes for lazy loaded images is enabled.
    	 */
    	if ( ! apply_filters( 'wp_img_tag_add_auto_sizes', true ) ) {
    		return $image;
    	}

    	$processor = new WP_HTML_Tag_Processor( $image );

    	// Bail if there is no IMG tag.
    	if ( ! $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ) {
    		return $image;
    	}

    	// Bail early if the image is not lazy-loaded.
    	$loading = $processor->get_attribute( 'loading' );
    	if ( ! is_string( $loading ) || 'lazy' !== strtolower( trim( $loading, " \t\f\r\n" ) ) ) {
    		return $image;
    	}

    	/*
    	 * Bail early if the image doesn't have a width attribute.
    	 * Per WordPress Core itself, lazy-loaded images should always have a width attribute.
    	 * However, it is possible that lazy-loading could be added by a plugin, where we don't have that guarantee.
    	 * As such, it still makes sense to ensure presence of a width attribute here in order to use `sizes=auto`.
    	 */
    	$width = $processor->get_attribute( 'width' );
    	if ( ! is_string( $width ) || '' === $width ) {
    		return $image;
    	}

    	$sizes = $processor->get_attribute( 'sizes' );

    	// Bail early if the image is not responsive.
    	if ( ! is_string( $sizes ) ) {
    		return $image;
    	}

    	// Don't add 'auto' to the sizes attribute if it already exists.
    	if ( wp_sizes_attribute_includes_valid_auto( $sizes ) ) {
    		return $image;
    	}

    	$processor->set_attribute( 'sizes', "auto, $sizes" );
    	return $processor->get_updated_html();
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/media.php#L2117)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/media.php#L2117-L2167)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#hooks)󠁿

 [apply_filters( ‘wp_img_tag_add_auto_sizes’, boolean $enabled )](https://developer.wordpress.org/reference/hooks/wp_img_tag_add_auto_sizes/)

Filters whether auto-sizes for lazy loaded images is enabled.

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_sizes_attribute_includes_valid_auto()](https://developer.wordpress.org/reference/functions/wp_sizes_attribute_includes_valid_auto/)`wp-includes/media.php` |

Checks whether the given ‘sizes’ attribute includes the ‘auto’ keyword as the first item in the list.

  | 
| [WP_HTML_Tag_Processor::__construct()](https://developer.wordpress.org/reference/classes/wp_html_tag_processor/__construct/)`wp-includes/html-api/class-wp-html-tag-processor.php` |

Constructor.

  | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#)

| Used by | Description | 
| [wp_filter_content_tags()](https://developer.wordpress.org/reference/functions/wp_filter_content_tags/)`wp-includes/media.php` |

Filters specific tags in post content and modifies their markup.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_img_tag_add_auto_sizes/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.7.0](https://developer.wordpress.org/reference/since/6.7.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_img_tag_add_auto_sizes%2F)
before being able to contribute a note or feedback.