Using WordPress ‘fallback_intermediate_image_sizes’ PHP filter

The fallback_intermediate_image_sizes WordPress PHP filter allows you to modify the image sizes generated for non-image mime types.

Usage

add_filter( 'fallback_intermediate_image_sizes', 'your_custom_function', 10, 2 );

function your_custom_function( $fallback_sizes, $metadata ) {
    // Your custom code here
    return $fallback_sizes;
}

Parameters

  • $fallback_sizes (string[]): An array of image size names.
  • $metadata (array): Current attachment metadata.

More information

See WordPress Developer Resources: fallback_intermediate_image_sizes

Examples

Removing a specific image size for non-image files

Remove the ‘medium’ image size for non-image files.

add_filter( 'fallback_intermediate_image_sizes', 'remove_medium_size_for_non_images', 10, 2 );

function remove_medium_size_for_non_images( $fallback_sizes, $metadata ) {
    $key = array_search( 'medium', $fallback_sizes );
    if ( false !== $key ) {
        unset( $fallback_sizes[ $key ] );
    }
    return $fallback_sizes;
}

Adding a custom image size for non-image files

Add a custom image size for non-image files.

add_filter( 'fallback_intermediate_image_sizes', 'add_custom_size_for_non_images', 10, 2 );

function add_custom_size_for_non_images( $fallback_sizes, $metadata ) {
    $fallback_sizes[] = 'custom-size';
    return $fallback_sizes;
}

Change all non-image files to a specific image size

Set all non-image files to use the ‘large’ image size.

add_filter( 'fallback_intermediate_image_sizes', 'set_large_size_for_non_images', 10, 2 );

function set_large_size_for_non_images( $fallback_sizes, $metadata ) {
    return array( 'large' );
}

Remove all image sizes for non-image files

Remove all image sizes for non-image files.

add_filter( 'fallback_intermediate_image_sizes', 'remove_all_sizes_for_non_images', 10, 2 );

function remove_all_sizes_for_non_images( $fallback_sizes, $metadata ) {
    return array();
}

Set different sizes based on file type

Set different image sizes for different file types.

add_filter( 'fallback_intermediate_image_sizes', 'set_sizes_based_on_file_type', 10, 2 );

function set_sizes_based_on_file_type( $fallback_sizes, $metadata ) {
    if ( isset( $metadata['filetype']['ext'] ) && 'pdf' === $metadata['filetype']['ext'] ) {
        return array( 'large', 'medium' );
    } else {
        return array( 'thumbnail', 'small' );
    }
}