Enable uploading AVIF Images on WordPress website
Enabling AVIF images on your WordPress website can significantly enhance your site’s loading speed and overall performance. AVIF (AV1 Image File Format) is a modern image format that offer superior compression efficiency compared to traditional formats like JPEG and PNG. This means they can make your website faster by reducing the size of the images it needs to load, without compromising on image quality.
So, in your WordPress theme add this code into functions.php
function custom_mime_types( $mime_types ) {
$mime_types['avif'] = 'image/avif'; // Adding .avif
return $mime_types;
}
add_filter( 'mime_types', 'custom_mime_types' );
function add_avif_upload_support( $checked, $file, $filename, $mimes ) {
if ( ! $checked['type'] ) {
$check_filetype = wp_check_filetype( $filename, $mimes );
$ext = $check_filetype['ext'];
$type = $check_filetype['type'];
$proper_filename = $filename;
if ( $type && 0 === strpos( $type, 'image/' ) && $ext !== 'svg' ) {
$checked = compact( 'ext', 'type', 'proper_filename' );
} else {
$checked = array( 'ext' => false, 'type' => false, 'proper_filename' => false );
}
}
return $checked;
}
add_filter( 'wp_check_filetype_and_ext', 'add_avif_upload_support', 10, 4 );
Now you should be able to upload AVIF images into your WordPress website with no problems!.