Snippets

Joseph Dickson enable webp - functions.php

Created by Joseph Dickson last modified
<?php
**
 * As of WordPress 5.8 webp is supported by default
 * Enable webp image format
 */
function jd_upload_mimes($existing_mimes) {
    $existing_mimes['webp'] = 'image/webp';
    return $existing_mimes;
}

add_filter('mime_types', 'jd_upload_mimes');

// Enable preview and thumbnail for webp image files
function jd_webp_is_displayable($result, $path) {
    if ($result === false) {
        $displayable_image_types = array( IMAGETYPE_WEBP );
        $info = @getimagesize( $path );

        if (empty($info)) {
            $result = false;
        } elseif (!in_array($info[2], $displayable_image_types)) {
            $result = false;
        } else {
            $result = true;
        }
    }

    return $result;
}
add_filter('file_is_displayable_image', 'jd_webp_is_displayable', 10, 2);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.