Skip to content

Instantly share code, notes, and snippets.

@salimhossain
Last active January 31, 2025 01:40
Show Gist options
  • Save salimhossain/ff3a32e405d9d2cbd634e71efdf2572b to your computer and use it in GitHub Desktop.
Save salimhossain/ff3a32e405d9d2cbd634e71efdf2572b to your computer and use it in GitHub Desktop.
WordPress Allow WebP uploads
<?php
// Allow WebP uploads
add_filter( 'mime_types', 'enable_webp_uploads' );
function enable_webp_uploads( $mimes ) {
$mimes['webp'] = 'image/webp';
return $mimes;
}
// Check WebP support
add_action( 'admin_notices', 'check_webp_support' );
function check_webp_support() {
if ( ! function_exists( 'wp_image_editor_supports' ) ) {
return;
}
$webp_supported = wp_image_editor_supports( array( 'mime_type' => 'image/webp' ) );
if ( ! $webp_supported ) {
echo '<div class="notice notice-error"><p>Warning: Your server does NOT support WebP thumbnail generation. Contact your hosting provider to enable GD/Imagick with WebP support.</p></div>';
} else {
echo '<div class="notice notice-success"><p>Success: Your server supports WebP thumbnail generation.</p></div>';
}
}
// Force WebP MIME type recognition
add_filter( 'wp_check_filetype_and_ext', 'fix_webp_mime_type', 10, 4 );
function fix_webp_mime_type( $types, $file, $filename, $mimes ) {
if ( false !== strpos( $filename, '.webp' ) ) {
$types['ext'] = 'webp';
$types['type'] = 'image/webp';
}
return $types;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment