Title: wp_img_tag_add_width_and_height_attr
Published: August 11, 2020
Last modified: August 20, 2026

---

# wp_img_tag_add_width_and_height_attr( string $image, string $context, int $attachment_id ): string

## In this article

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

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

Adds `width` and `height` attributes to an `img` HTML tag.

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

 `$image`stringrequired

The HTML `img` tag where the attribute should be added.

`$context`stringrequired

Additional context to pass to the filters.

`$attachment_id`intrequired

Image attachment ID.

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

 string Converted `'img'` element with `'width'` and `'height'` attributes added.

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

    ```php
    function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id ) {
    	$image_src         = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
    	list( $image_src ) = explode( '?', $image_src );

    	// Return early if we couldn't get the image source.
    	if ( ! $image_src ) {
    		return $image;
    	}

    	/**
    	 * Filters whether to add the missing `width` and `height` 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.
    	 */
    	$add = apply_filters( 'wp_img_tag_add_width_and_height_attr', true, $image, $context, $attachment_id );

    	if ( true === $add ) {
    		$image_meta = wp_get_attachment_metadata( $attachment_id );
    		$size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id );

    		if ( $size_array && $size_array[0] && $size_array[1] ) {
    			// If the width is enforced through style (e.g. in an inline image), calculate the dimension attributes.
    			$style_width = preg_match( '/style="width:\s*(\d+)px;"/', $image, $match_width ) ? (int) $match_width[1] : 0;
    			if ( $style_width ) {
    				$size_array[1] = (int) round( $size_array[1] * $style_width / $size_array[0] );
    				$size_array[0] = $style_width;
    			}

    			$hw = trim( image_hwstring( $size_array[0], $size_array[1] ) );
    			return str_replace( '<img', "<img {$hw}", $image );
    		}
    	}

    	return $image;
    }
    ```

[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#L2372)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/media.php#L2372-L2413)

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

 [apply_filters( ‘wp_img_tag_add_width_and_height_attr’, bool $value, string $image, string $context, int $attachment_id )](https://developer.wordpress.org/reference/hooks/wp_img_tag_add_width_and_height_attr/)

Filters whether to add the missing `width` and `height` HTML attributes to the img
tag. Default `true`.

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

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

Determines an image’s width and height dimensions based on the source file.

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

Retrieves width and height attributes using given width and height values.

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

Retrieves attachment metadata for attachment ID.

  | 
| [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_width_and_height_attr/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/wp_img_tag_add_width_and_height_attr/?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_width_and_height_attr/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.5.0](https://developer.wordpress.org/reference/since/5.5.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_width_and_height_attr%2F)
before being able to contribute a note or feedback.