WordPress plugin-free implementation: Automatically convert uploaded images to WebP format (essential for website acceleration 🔥)

whyWordpressNeed WebP image optimization?

In today's Internet environment where speed is key,WordPress Image OptimizationIt has become a compulsory course for website acceleration.

Traditional JPEG, PNG, and GIF images are large in size, slow to load, and provide a poor user experience.

WebP image formatLaunched by Google, it has the following advantages:

  1. Smaller file sizes → Improve website access speed and reduce bounce rate.
  2. Image quality is not lagging behind → Still clear and delicate after compression.
  3. SEOfriendly → WordPress websites are more likely to get higher rankings on search engines such as Google and Baidu.

in other words,WordPress WebP Optimization = Fast speed + high ranking + more satisfied users.

Pain points of traditional image compression in WordPress

Most webmasters do this:

  • For the first time TinyPNG Or Squoosh Manually compress and convert to WebP.
  • Then upload it to your WordPress website.

The problem is - this process is like doing boring brick-moving ⚒️.

Not only is it time-consuming, it also significantly reduces the efficiency of writing articles. Moreover, relying on plugins either has limited quotas or too many plugins can cause WordPress to become slow.

WordPress plugin-free automatic conversion of PHP code to WebP

WordPress plugin-free implementation: Automatically convert uploaded images to WebP format (essential for website acceleration 🔥)

The solution is here!

Through the following paragraph PHP code, which can automatically convert JPG, JPEG, PNG, and GIF into WebP when uploading, completely freeing your hands.

/**
 * Convert Uploaded Images to WebP Format with Custom Quality
 */
add_filter('wp_handle_upload', 'wpturbo_handle_upload_convert_to_webp');
function wpturbo_handle_upload_convert_to_webp($upload) {
    if (in_array($upload['type'], ['image/jpeg', 'image/png', 'image/gif'])) {
        $file_path = $upload['file'];
        if (extension_loaded('imagick') || extension_loaded('gd')) {
            $image_editor = wp_get_image_editor($file_path);
            if (!is_wp_error($image_editor)) {
                $quality = 80; // 建议范围 70-90
                $image_editor->set_quality($quality);

                $file_info = pathinfo($file_path);
                $dirname = $file_info['dirname'];
                $filename = $file_info['filename'];
                $def_filename = wp_unique_filename($dirname, $filename . '.webp');
                $new_file_path = $dirname . '/' . $def_filename;

                $saved_image = $image_editor->save($new_file_path, 'image/webp');

                if (!is_wp_error($saved_image) && file_exists($saved_image['path'])) {
                    $upload['file'] = $saved_image['path'];
                    $upload['url'] = str_replace(basename($upload['url']), basename($saved_image['path']), $upload['url']);
                    $upload['type'] = 'image/webp';
                    @unlink($file_path); // 假如要保留原图,请注释这行
                }
            }
        }
    }
    return $upload;
}

👉 Recommended by Code Snippets Or WP Code WordPress pluginTo manage this code, it will not affect the WordPress theme upgrade.

by WordPress plugin-free WebP image optimization, you can easily achieve:

  • Compress as soon as you upload → Save a lot of time
  • Faster loading speed → Improved user experience
  • Better SEO performance → Higher website ranking

Actual test results: How strong is WordPress WebP compression?

  • Before uploading: PNG format, size 975 KB
  • After uploading: Automatically convert to WebP, the size is only 35 KB 🎉
  • Warm reminder: The specific compression size is subject to the actual compression situation.

Compression ratio up to 80% or more, the loading speed is so fast🚀, users can hardly feel the decline in image quality.

WordPress image compression automation makes your website run faster than the wind! 🌪️

Comment

Your email address will not be published. Required fields * Callout

Scroll to Top