Last active
January 31, 2025 01:40
-
-
Save salimhossain/ff3a32e405d9d2cbd634e71efdf2572b to your computer and use it in GitHub Desktop.
WordPress Allow WebP uploads
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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