AI Verified

Name

Convert Uploaded Images to WebP Format

About

This snippet converts uploaded images (JPEG, PNG, GIF) to WebP format * automatically in WordPress. Ideal for use in a theme's functions.php file, * or with plugins like Code Snippets or WPCodeBox.

Language

PHP

Rating

Voted: 2 by 2 user(s)

Link for further information:

The author has provided the following URL that may be helpful to setting up or using this snippet:

https://www.youtube.com/watch?v=bYz6lDy7Sok

Codevault

duke_flambo

Scroll down to see more snippets from this codevault.

Wordpress Compatability

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

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.

Snippet Source:

https://codesnippets.cloud/snippet/Super-Snippet-Storage/Convert-To-339

History

Last modified:

04/07/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 : 20
Code : unlink( $file_path )
Vulnerable line : 29
Code : $file_path = $upload['file']);

Found 2 vulnerabilities

Convert Uploaded Images to WebP Format

 
                    
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 * @author 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 new file path for the WebP image
40 $new_file_path = $dirname . '/' . $filename . '.webp';
41 
42 // Attempt to save the image in WebP format
43 $saved_image = $image_editor->save( $new_file_path, 'image/webp' );
44 if ( ! is_wp_error( $saved_image ) && file_exists( $saved_image['path'] ) ) {
45 // Success: replace the uploaded image with the WebP image
46 $upload['file'] = $saved_image['path'];
47 $upload['url'] = str_replace( basename( $upload['url'] ), basename( $saved_image['path'] ), $upload['url'] );
48 $upload['type'] = 'image/webp';
49 
50 // Optionally remove the original image
51 @unlink( $file_path );
52 }
53 }
54 }
55 }
56 
57 return $upload;
58}

2

Related Snippets

Please see some snippets below related to this snippet..

WordPress Admin

AI Verified

2

Company Screen Option

Added: 7 months ago

Last Updated: 4 months ago

Add your Logo and Company Details to the WordPress Dashboard as a Screen Option

WordPress Admin

AI Verified

0

Post and page duplication

Added: 6 months ago

Last Updated: 6 months ago

Snippet to add functionality to duplicate posts and pages. This was inspired by the post duplication snippet found on codesnippets.cloud (see link)

WordPress Admin

AI Verified

0

remove options menu e snipettcode menu

Added: 1 year ago

Last Updated: 1 year ago

remove the options and code snippets menu for non-admin users if they have "manage_options" capability.

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

WordPress Admin

AI Verified

2

Convert Uploaded Images to WebP Format

Added: 7 months ago

Last Updated: 3 days ago

This snippet converts uploaded images (JPEG, PNG, GIF) to WebP format * automatically in WordPress. Ideal for use in a theme's functions.php file, * or with plugins like Code Snippets or WPCodeBox...

General

AI Verified

1

Remove WP media images from being created for no reason

Added: 6 months ago

Last Updated: 1 day ago

WordPress Admin

AI Verified

0

Company Details

Added: 7 months ago

Last Updated: 7 months ago

Custom Dashboard Widget for WordPress Admin