Skip to main content

How to allow ".WEBP" images in WordPress by coding

 Normally, WordPress does not allow to uploading of .webp format image files. But in some cases, we need to use that format. In order to allow the format to be used in WordPress, we need to add some coding in functions.php in the active theme. The following code needs to be added in the bottom of all codes in functions.php in the active theme.

function webp_upload_mimes( $existing_mimes ) { // add webp to the list of mime types $existing_mimes['webp'] = 'image/webp'; // return the array back to the function with our added mime type return $existing_mimes; } add_filter( 'mime_types', 'webp_upload_mimes' );

These lines of code will allow uploading .webp format images but the thumbnail could not be seen from the WordPress dashboard. In order to view the thumbnail, we need to add an additional piece of code at just the bottom of the above code.

//** * Enable preview / thumbnail for webp image files.*/ function 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', 'webp_is_displayable', 10, 2);

Please note: These codes must have to be in functions.php in active theme.

Comments

Popular posts from this blog

Set WordPress custom excerpt from Rank Math SEO plugin

  Sometimes, we need to set different excerpts for showing posts on the blog page. In this post, we are going to share a piece of code that works perfectly to change a posts' excerpt programmatically.  function rankmath_post_excerpt(){         $id = get_the_ID();         $post_type = get_post_type( get_the_ID() );         if ($post_type == 'post') {             $meta_description = get_post_meta($id,'rank_math_description',true);             return $meta_description;          }else{             return;         }          } function check_for_rankmath_activation(){     if ( is_plugin_active( 'seo-by-rank-math/rank-math.php' ) ){         add_filter('get_the_excerpt', 'rankmath_post_excerpt' );         add_filter('t...

Hide a product from WooCommerce main shop page

 function custom_pre_get_posts_query( $q ) {     $tax_query = (array) $q->get( 'tax_query' );     $tax_query[] = array(            'taxonomy' => 'product_cat',            'field' => 'slug',            'terms' => array( 'buster' ), // Don't display products in the clothing category on the shop page.            'operator' => 'NOT IN'     );     if(is_shop()){     $q->set( 'tax_query', $tax_query );   }else{     return;   }      } add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );