The getimagesize_mimes_to_exts WordPress PHP filter allows you to modify the list of image mime types and their respective extensions.
Usage
add_filter('getimagesize_mimes_to_exts', 'your_custom_function');
function your_custom_function($mime_to_ext) {
// your custom code here
return $mime_to_ext;
}
Parameters
- $mime_to_ext (array): An array containing the image mime types and their matching extensions.
More information
See WordPress Developer Resources: getimagesize_mimes_to_exts
Examples
Add a new mime type and extension
Add the WebP format to the list of supported image formats:
add_filter('getimagesize_mimes_to_exts', 'add_webp_mime_type');
function add_webp_mime_type($mime_to_ext) {
$mime_to_ext['image/webp'] = 'webp';
return $mime_to_ext;
}
Remove a mime type and extension
Remove support for GIF images:
add_filter('getimagesize_mimes_to_exts', 'remove_gif_mime_type');
function remove_gif_mime_type($mime_to_ext) {
unset($mime_to_ext['image/gif']);
return $mime_to_ext;
}
Change the extension for a mime type
Change the extension for JPEG images to ‘.jpeg’ instead of ‘.jpg’:
add_filter('getimagesize_mimes_to_exts', 'change_jpeg_extension');
function change_jpeg_extension($mime_to_ext) {
$mime_to_ext['image/jpeg'] = 'jpeg';
return $mime_to_ext;
}
Add multiple mime types and extensions
Add support for both AVIF and WebP image formats:
add_filter('getimagesize_mimes_to_exts', 'add_avif_webp_mime_types');
function add_avif_webp_mime_types($mime_to_ext) {
$mime_to_ext['image/avif'] = 'avif';
$mime_to_ext['image/webp'] = 'webp';
return $mime_to_ext;
}
Modify the entire mime type and extension list
Replace the default mime types and extensions list with a custom list:
add_filter('getimagesize_mimes_to_exts', 'custom_mime_types_and_extensions');
function custom_mime_types_and_extensions($mime_to_ext) {
$mime_to_ext = array(
'image/jpeg' => 'jpg',
'image/png' => 'png',
'image/webp' => 'webp'
);
return $mime_to_ext;
}