Get an image URL from a WordPress attachment object for a specific size.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
attachment | object | The WordPress attachment object | |
size | string | The desired image size (e.g., 'thumbnail', 'medium', 'full') | |
fallbackSize | string | <optional> | The fallback size if the requested size isn't available |
- Since
- 4.0.3
Returns:
The URL of the requested image size, or the fallback size, or the base URL.
- Type:
- string
Example
import { getAttachmentImageUrl } from '@gravityforms/utils';
const attachment = {
id: 123,
url: 'https://example.com/image.jpg',
sizes: {
thumbnail: {
url: 'https://example.com/image-thumbnail.jpg',
},
medium: {
url: 'https://example.com/image-medium.jpg',
},
},
};
const imageUrl = getAttachmentImageUrl( attachment, 'thumbnail' );
console.log( imageUrl ); // 'https://example.com/image-thumbnail.jpg'