Images are a major factor in website performance. WebP is a modern image format that provides superior compression and quality compared to JPEG and PNG. Manually converting images can be tedious, especially if you upload multiple images frequently.
In this tutorial, we will show you how to automatically convert WordPress image uploads to WebP using custom PHP code in your functions.php file. This ensures that JPEG and PNG images are converted to WebP automatically, while WebP files uploaded directly remain unchanged.
Why Use WebP in WordPress?
- Reduces image file size significantly.
- Improves page load speed and Core Web Vitals.
- Maintains high image quality.
- Supported by all modern browsers.
PHP Extensions Required for Automatic WebP Conversion
To convert images to WebP in PHP, your server must have at least one of these extensions:
- GD Library with WebP support
- Imagick (ImageMagick PHP extension)
If neither extension is available, the code will safely skip conversion and store the uploaded image in its original format (JPEG/PNG). Most modern hosting, including MAMP, have GD enabled by default. Imagick is available in MAMP Pro and some shared hosts.
How to Check if GD or Imagick is Enabled
<?php
phpinfo();
?>
Look for the GD and Imagick sections. Ensure WebP Support is enabled under GD, and WEBP is listed under supported formats for Imagick.
Custom Code to Automatically Convert Uploads in functions.php
Add the following code at the end of your functions.php file in your active theme. It handles both GD and Imagick, converts JPEG/PNG to WebP at 80% quality, and keeps WebP files unchanged.
<?php
// Auto-convert WordPress uploads to WebP if GD or Imagick is available
add_filter('wp_handle_upload', 'maybe_convert_to_webp');
function maybe_convert_to_webp($upload) {
$file = $upload['file'];
$info = pathinfo($file);
$ext = strtolower($info['extension']);
// Only attempt conversion for JPEG/PNG
if (!in_array($ext, ['jpg', 'jpeg', 'png'])) {
return $upload; // Skip WebP or other formats
}
$can_convert = false;
// Check if GD or Imagick is available
if (function_exists('imagewebp')) {
$can_convert = 'gd';
} elseif (class_exists('Imagick')) {
$can_convert = 'imagick';
}
// If no supported library, skip conversion
if (!$can_convert) {
return $upload;
}
// WebP file path
$webp_file = $info['dirname'] . '/' . $info['filename'] . '.webp';
if ($can_convert === 'gd') {
// Load image using GD
switch ($ext) {
case 'jpg':
case 'jpeg':
$image = imagecreatefromjpeg($file);
break;
case 'png':
$image = imagecreatefrompng($file);
imagepalettetotruecolor($image);
imagealphablending($image, true);
imagesavealpha($image, true);
break;
}
// Save as WebP with 80% quality
imagewebp($image, $webp_file, 80);
imagedestroy($image);
} elseif ($can_convert === 'imagick') {
// Load image using Imagick
$img = new Imagick($file);
$img->setImageFormat('webp');
$img->setImageCompressionQuality(80);
$img->writeImage($webp_file);
$img->clear();
$img->destroy();
}
// Delete original file
if (file_exists($file)) {
unlink($file);
}
// Update upload array to use WebP
$upload['file'] = $webp_file;
$upload['url'] = str_replace(basename($file), basename($webp_file), $upload['url']);
$upload['type'] = 'image/webp';
return $upload;
}
?>
How the Code Works
- Checks if the uploaded file is JPEG or PNG.
- Verifies if GD or Imagick is available.
- Converts the image to WebP at 80% quality using the available library.
- Deletes the original JPEG/PNG file.
- Updates WordPress upload array to point to the WebP file.
Handling Bulk Uploads
WordPress calls this function for each uploaded file individually. You can safely upload 20, 30, or more images at once. Keep in mind:
- Large file sizes may require increasing
memory_limitandmax_execution_timeinphp.ini. - Shared hosting may slow down if uploading many high-resolution images simultaneously.
Conclusion
With this custom PHP code in functions.php, your WordPress site on Codenbrand will automatically convert JPEG and PNG uploads to WebP. Directly uploaded WebP files are stored as-is, avoiding unnecessary compression. The code works with both GD and Imagick and safely falls back to the original image if neither extension is available.
This solution eliminates the headache of manual WebP conversion, ensures your media library is optimized, and improves website performance effortlessly.