Using WordPress ‘get_intermediate_image_sizes()’ PHP function

The get_intermediate_image_sizes() WordPress PHP function retrieves the available intermediate image size names.

Usage

$image_sizes = get_intermediate_image_sizes();

Parameters

  • None

More information

See WordPress Developer Resources: get_intermediate_image_sizes()

Examples

Display all available image sizes

This code snippet retrieves and displays all the available image sizes.

$image_sizes = get_intermediate_image_sizes();

foreach ($image_sizes as $size) {
    echo $size . '<br>';
}

Check if a specific image size exists

This example checks if the ‘medium_large’ image size is available.

$image_sizes = get_intermediate_image_sizes();

if (in_array('medium_large', $image_sizes)) {
    echo "medium_large size exists!";
} else {
    echo "medium_large size does not exist!";
}

Add a custom image size and display all sizes

This example adds a custom image size named ‘custom_size’ and then displays all the available image sizes.

add_image_size('custom_size', 400, 400, true);
$image_sizes = get_intermediate_image_sizes();

foreach ($image_sizes as $size) {
    echo $size . '<br>';
}

Retrieve image URLs for a specific attachment

This example gets the URLs for all available sizes of an attachment with a specific attachment ID.

$attachment_id = 123;
$image_sizes = get_intermediate_image_sizes();

foreach ($image_sizes as $size) {
    $image_url = wp_get_attachment_image_src($attachment_id, $size);
    echo $size . ': ' . $image_url[0] . '<br>';
}

Display image URLs for a specific post

This example retrieves and displays the URLs of all available image sizes for a specific post’s featured image.

$post_id = 456;
$attachment_id = get_post_thumbnail_id($post_id);
$image_sizes = get_intermediate_image_sizes();

foreach ($image_sizes as $size) {
    $image_url = wp_get_attachment_image_src($attachment_id, $size);
    echo $size . ': ' . $image_url[0] . '<br>';
}