Home / Attachments / Allow WebP Files Upload
Duplicate Snippet

Embed Snippet on Your Site

Allow WebP Files Upload

The provided PHP code is intended to enable the upload of WebP image files in WordPress by adding support for the WebP file format.

Code Preview
php
<?php
// Add WebP MIME type support
function add_webp_mime_type($mime_types) {
    $mime_types['webp'] = 'image/webp';
    return $mime_types;
}
add_filter('upload_mimes', 'add_webp_mime_type');
// Enable WebP in the Media Library
function enable_webp_upload($data, $file) {
    $file['ext'] = 'webp';
    return $data;
}
add_filter('wp_handle_upload_prefilter', 'enable_webp_upload', 10, 2);
// Enable WebP thumbnails
function display_webp_media($result, $path) {
    if ($result === false) {
        $info = pathinfo($path);
        $ext = $info['extension'];
        if ($ext === 'webp') {
            return array('ext' => 'jpg', 'mime-type' => 'image/jpeg');
        }
    }
    return $result;
}
add_filter('file_is_displayable_image', 'display_webp_media', 10, 2);

Comments

Add a Comment