Get an image URL from a WordPress attachment object for a specific size.

Parameters:
NameTypeAttributesDescription
attachmentobject

The WordPress attachment object

sizestring

The desired image size (e.g., 'thumbnail', 'medium', 'full')

fallbackSizestring<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'