Title: image_hwstring
Published: April 25, 2014
Last modified: May 20, 2026

---

# image_hwstring( int|string $width, int|string $height ): string

## In this article

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

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

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

## 󠀁[Description](https://developer.wordpress.org/reference/functions/image_hwstring/?output_format=md#description)󠁿

Both attributes are required in the sense that both parameters must have a value,
but are optional in that if you set them to false or null, then they will not be
added to the returned string.

You can set the value using a string, but it will only take numeric values.
If you
wish to put ‘px’ after the numbers, then it will be stripped out of the return.

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

 `$width`int|stringrequired

Image width in pixels.

`$height`int|stringrequired

Image height in pixels.

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

 string HTML attributes for width and, or height.

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

    ```php
    function image_hwstring( $width, $height ) {
    	$out = '';
    	if ( $width ) {
    		$out .= 'width="' . (int) $width . '" ';
    	}
    	if ( $height ) {
    		$out .= 'height="' . (int) $height . '" ';
    	}
    	return $out;
    }
    ```

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

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

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

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

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

Gets an img tag for an image attachment, scaling it down if requested.

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

Gets an HTML img element representing an image attachment.

  |

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

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

## User Contributed Notes

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