AI Verified

Name

Convert To WebP

About

<p>Snippet to convert JPG / PNG / Gif to WebP automatically on upload. Used GD or ImageMagick</p>

Language

PHP

Rating

Voted: 17 by 21 user(s)

Codevault

Super-Snippet-Storage

Scroll down to see more snippets from this codevault.

Wordpress Compatability

The author has indicated that this snippet is compatable up to wordpress version: Not Specified

Our AI bot has checked this snippet is compatable up to wordpress version: 6.1

Code Snippet Plugin Sync

Free & Pro

Download this snippet by clicking the download button, then head over to the Code Snippet Plugin settings in your wordpress admin dashboard, select the import menu then upload this file to import into your wordpress site.

Pro Only (Coming Soon)

You will be able to click a button and sync this snippet to your wordpress site automatically and from your dashboard manage all code snippets across all your wordpress sites that have the Code Snippets Pro plugin installed.

History

Last modified:

08/06/2024

Important Note

This snippet has the following status:

AI Verified

This snippet has been tested by our AI bot, see any comments below.

AI Bot Comments:

Potential vulnerability found : Arbitrary File Deletion
Found on line : 20
Code : unlink( $file_path )
Vulnerable line : 29
Code : $file_path = $upload['file']);

Potential vulnerability found : Arbitrary File Deletion
Found on line : 51
Code : unlink($file_path)
Vulnerable line : 29
Code : $file_path = $upload['file']);

Found 2 vulnerabilities

Convert To WebP

 
                    
1/**
2 * Convert Uploaded Images to WebP Format
3 *
4 * This snippet converts uploaded images (JPEG, PNG, GIF) to WebP format
5 * automatically in WordPress. Ideal for use in a theme's functions.php file,
6 * or with plugins like Code Snippets or WPCodeBox.
7 *
8 * @package WordPress_Custom_Functions
9 * @autor Mark Harris
10 * @link www.christchurchwebsolutions.co.uk
11 *
12 * Usage Instructions:
13 * - Add this snippet to your theme's functions.php file, or add it as a new
14 * snippet in Code Snippets or WPCodeBox.
15 * - The snippet hooks into WordPress's image upload process and converts
16 * uploaded images to the WebP format.
17 *
18 * Optional Configuration:
19 * - By default, the original image file is deleted after conversion to WebP.
20 * If you prefer to keep the original image file, simply comment out or remove
21 * the line '@unlink( $file_path );' in the wpturbo_handle_upload_convert_to_webp function.
22 * This will preserve the original uploaded image file alongside the WebP version.
23 */
24 
25add_filter('wp_handle_upload', 'wpturbo_handle_upload_convert_to_webp');
26 
27function wpturbo_handle_upload_convert_to_webp($upload) {
28 if ($upload['type'] == 'image/jpeg' || $upload['type'] == 'image/png' || $upload['type'] == 'image/gif') {
29 $file_path = $upload['file'];
30 
31 // Check if ImageMagick or GD is available
32 if (extension_loaded('imagick') || extension_loaded('gd')) {
33 $image_editor = wp_get_image_editor($file_path);
34 if (!is_wp_error($image_editor)) {
35 $file_info = pathinfo($file_path);
36 $dirname = $file_info['dirname'];
37 $filename = $file_info['filename'];
38 
39 // Create a unique file path for the WebP image
40 $def_filename = wp_unique_filename($dirname, $filename . '.webp');
41 $new_file_path = $dirname . '/' . $def_filename;
42 
43 // Attempt to save the image in WebP format
44 $saved_image = $image_editor->save($new_file_path, 'image/webp');
45 if (!is_wp_error($saved_image) && file_exists($saved_image['path'])) {
46 // Success: replace the uploaded image with the WebP image
47 $upload['file'] = $saved_image['path'];
48 $upload['url'] = str_replace(basename($upload['url']), basename($saved_image['path']), $upload['url']);
49 $upload['type'] = 'image/webp';
50 
51 // Optionally remove the original image
52 @unlink($file_path);
53 }
54 }
55 }
56 }
57 
58 return $upload;
59}

17

Comments

  • L

    6 months ago

    Is it possible to add the IPTC fields of the original file (jpg) to the converted file in the database, or to get the wp_post ID of the new file?
  • L

    6 months ago

    wp_get_attachment_metadata not working.
    
  • 2 months ago

    It doesn't work for me anymore, it used to work
  • R

    1 month ago

    It not manage name duplication but override the file
  • R

    1 month ago

    Fixed the wrong overwriting error editing line 40 ($new_file_path = $dirname . '/' . $filename . '.webp';) with these two lines:
    				$def_filename = wp_unique_filename($dirname, $filename . '.webp' );
                    $new_file_path = $dirname . '/' . $def_filename;
  • MH

    2 weeks ago

    @Rightnow I have updated the original code with your change. Thanks :)

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

0

Allow SVG Files Upload

Added: 8 months ago

Last Updated: 8 months ago

Add support for SVG files to be uploaded in WordPress media.

General

AI Verified

2

Default featured image

Added: 1 year ago

Last Updated: 1 year ago

This code defines a function named default_post_metadata__thumbnail_id that modifies the value of the _thumbnail_id metadata field for a post. The function takes five arguments: $value: The current...

General

AI Verified

0

Cerrar sesion - cliente

Added: 5 months ago

Last Updated: 5 months ago

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

17

Convert To WebP

Added: 6 months ago

Last Updated: 1 week ago

<p>Snippet to convert JPG / PNG / Gif to WebP automatically on upload. Used GD or ImageMagick</p>

WordPress Admin

AI Verified

5

Really Simple Duplications

Added: 6 months ago

Last Updated: 4 months ago

A snippet to duplicate posts and pages and CPTS.

General

AI Verified

2

WP-Admin ChatGPT

Added: 3 months ago

Last Updated: 4 weeks ago