...

Conversion images to WebP format (for WordPress)

Image optimization is essential to improve website performance and improve SEO rankings. WebP, a modern image format developed by Google, provides better compression and quality than JPEG, PNG and GIF. By converting images to this format, you can speed up your WordPress site without relying on additional plugins.

This guide provides a simple method to automatically convert images to WebP format using custom PHP code in WordPress.

Why Use WebP Images in WordPress?

Faster Load Times – WebP images are up to 34% smaller than JPEGs and PNGs, reducing page load time.
Improved SEO Performance – Faster websites rank higher on search engines.
Better User Experience – Visitors stay longer on fast-loading sites, improving engagement and conversion rates.
No Additional Plugins Required – Save server resources and reduce plugin dependencies.

How to Convert Images to WebP in WordPress (Without Plugins)

Follow this step-by-step guide to enable automatic WebP conversion for uploaded images:

Step 1: Access the WordPress Theme Editor

  1. Log in to your WordPress admin panel.
  2. Navigate to Appearance > Theme Editor (or Tools > Theme File Editor in newer versions).
  3. Select the functions.php file from your active theme.

Step 2: Add the WebP Conversion Code

Copy and paste the following PHP code into your functions.php file:

add_filter( 'wp_handle_upload', 'wpturbo_handle_upload_convert_to_webp' );

function wpturbo_handle_upload_convert_to_webp( $upload ) {
if ( $upload['type'] == 'image/jpeg' || $upload['type'] == 'image/png' || $upload['type'] == 'image/gif' ) {
$file_path = $upload['file'];

// Check if ImageMagick or GD is available
if ( extension_loaded( 'imagick' ) || extension_loaded( 'gd' ) ) {
$image_editor = wp_get_image_editor( $file_path );
if ( ! is_wp_error( $image_editor ) ) {
$file_info = pathinfo( $file_path );
$dirname = $file_info['dirname'];
$filename = $file_info['filename'];

// Create a new file path for the WebP image
$new_file_path = $dirname . '/' . $filename . '.webp';

// Attempt to save the image in WebP format
$saved_image = $image_editor->save( $new_file_path, 'image/webp' );
if ( ! is_wp_error( $saved_image ) && file_exists( $saved_image['path'] ) ) {
// Success: replace the uploaded image with the WebP image
$upload['file'] = $saved_image['path'];
$upload['url'] = str_replace( basename( $upload['url'] ), basename( $saved_image['path'] ), $upload['url'] );
$upload['type'] = 'image/webp';

// Optionally remove the original image
@unlink( $file_path );
}
}
}
}

return $upload;
}

 

Step 3: Save and Apply Changes

  • Click Update File to save the changes.
  • Now, every time you upload a JPEG, PNG, or GIF image to the WordPress Media Library, it will be automatically converted to WebP.

Customizing the Code for Specific Image Formats

If you want to exclude a specific format from conversion (e.g., keep PNG images unchanged), modify this line in the code:

if ( $upload['type'] == 'image/jpeg' || $upload['type'] == 'image/png' || $upload['type'] == 'image/gif' ) {

 

For example, to skip PNG conversion, update it to:

if ( $upload['type'] == 'image/jpeg' || $upload['type'] == 'image/gif' ) {

 

Key Benefits of This Method

Universal Compatibility – Works with all WordPress versions.
Lightweight & Secure – No need for extra plugins, reducing security risks.
Efficient Performance – Uses either ImageMagick or GD library, ensuring compatibility.

By implementing this code, you automate WebP conversion and significantly improve your website’s speed and performance. 🚀

Enjoy faster load times and better SEO with WebP images in WordPress!

0 0 votes
Article rating
Subscribe
Notify of
0 comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

Login to your account