Using WordPress ‘image_hwstring()’ PHP function

The image_hwstring() WordPress PHP function retrieves width and height attributes using given width and height values.

Usage

To use the image_hwstring() function, provide the width and height values as integers or strings:

echo image_hwstring(300, 200);

Output:

width="300" height="200"

Parameters

  • $width (int|string): Required. Image width in pixels.
  • $height (int|string): Required. Image height in pixels.

More information

See WordPress Developer Resources: image_hwstring

Examples

Basic usage

Retrieve width and height attributes for an image with a width of 500px and a height of 400px:

echo image_hwstring(500, 400);

Using strings as parameters

You can also use strings as input, but only numeric values are accepted:

echo image_hwstring('600', '450');

Stripping out ‘px’ from input values

If you provide the width and height values with ‘px’, it will be stripped out of the return:

echo image_hwstring('700px', '350px');

Ignoring non-numeric values

Non-numeric values will be ignored, and the function will return an empty string:

echo image_hwstring('width', 'height');

Using the function with an HTML image tag

Combine the image_hwstring() function with an HTML image tag to set the width and height attributes:

echo '<img src="image.jpg" ' . image_hwstring(800, 600) . ' alt="Example Image">';