25% off ProSNIPC25OFF

Allow WebP and AVIF Image Uploads

Adds WebP and AVIF to WordPress allowed upload MIME types so modern image formats can be uploaded through the media library.

PHPby SnipCraft
php
<?php
if ( ! function_exists( 'scseed_allow_modern_image_types' ) ) {
    function scseed_allow_modern_image_types( $mimes ) {
        $mimes['webp'] = 'image/webp';
        $mimes['avif'] = 'image/avif';
        return $mimes;
    }
    add_filter( 'upload_mimes', 'scseed_allow_modern_image_types' );
}

if ( ! function_exists( 'scseed_fix_modern_image_mime_check' ) ) {
    function scseed_fix_modern_image_mime_check( $data, $file, $filename, $mimes ) {
        if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
            return $data;
        }
        $ext     = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) );
        $allowed = array(
            'webp' => 'image/webp',
            'avif' => 'image/avif',
        );
        if ( isset( $allowed[ $ext ] ) ) {
            $data['ext']  = $ext;
            $data['type'] = $allowed[ $ext ];
        }
        return $data;
    }
    add_filter( 'wp_check_filetype_and_ext', 'scseed_fix_modern_image_mime_check', 10, 4 );
}
#images#media#mime-types#upload