AI Verified

Name

Disable and Delete Image Sizes Generated by WordPress

About

This WordPress code snippet optimizes image handling by disabling automatic generation of various image sizes during uploads and removing existing generated sizes, thereby enhancing site performance and reducing unnecessary storage usage.

Language

PHP

Rating

Voted: 0 by 0 user(s)

How to Setup Snippet

I/ Using Theme's functions.php File 1. Log in to your WordPress admin. Go to "Appearance" -> "Theme Editor." 2. Find and select the functions.php file. 3. Scroll to the bottom and paste the code. 4. Click "Update File" to save. II/ Using a code snippet plugin 1. Install and activate your preferred plugin. 2. Find the plugin in the WordPress dashboard. 3. Create a new snippet. Paste the code and save. 4. Activate the snippet to enable the functionality. Remember to test changes on a staging site or backup before applying on a live site.

Codevault

laurentf

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.

History

Last modified:

02/12/2023

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 : File Inclusion / Path Traversal
Found on line : -1
Code : file(= $upload_dir['basedir'] . '/' . $path['dirname'] . '/' . $size_data['file'])
Vulnerable line : 20
Code : $path = pathinfo($meta['file']));

Potential vulnerability found : File Inclusion / Path Traversal
Found on line : 37
Code : file($intermediate_file)
Vulnerable line : 30
Code : $intermediate_file = $upload_dir['basedir'] . '/' . $path['dirname'] . '/' . $size_data['file']);

Found 2 vulnerabilities

Disable and Delete Image Sizes Generated by WordPress

 
                    
1// Disable automatic generation of different image sizes
2function disable_generated_image_sizes($sizes, $attachment_id) {
3 // Delete all automatically generated image sizes for an attachment
4 delete_generated_image_sizes($attachment_id);
5 
6 // Remove all automatically generated image sizes during upload
7 return array();
8}
9add_filter('intermediate_image_sizes_advanced', 'disable_generated_image_sizes', 10, 2);
10 
11// Define a function to delete generated image sizes for an attachment
12function delete_generated_image_sizes($attachment_id) {
13 // Get metadata for the attachment
14 $meta = wp_get_attachment_metadata($attachment_id);
15 
16 // Get upload directory information
17 $upload_dir = wp_upload_dir();
18 
19 // Get path information for the original file
20 $path = pathinfo($meta['file']);
21 
22 // Check if the dirname is outside the upload directory
23 if (strpos($path['dirname'], $upload_dir['basedir']) !== 0) {
24 return; // Stop execution if the dirname is outside the upload directory
25 }
26 
27 // Loop through each generated size of the image
28 foreach ($meta['sizes'] as $size => $size_data) {
29 // Construct the path to the generated size file
30 $intermediate_file = $upload_dir['basedir'] . '/' . $path['dirname'] . '/' . $size_data['file'];
31 
32 // Skip if the intermediate file is outside the upload directory
33 if (strpos($intermediate_file, $upload_dir['basedir']) !== 0) {
34 continue; // Skip if the intermediate file is outside the upload directory
35 }
36 
37 // Delete the intermediate file
38 wp_delete_file($intermediate_file);
39 }
40}
41 
42// Hook the function to the 'delete_attachment' action in WordPress
43add_action('delete_attachment', 'delete_generated_image_sizes');

0

Related Snippets

Please see some snippets below related to this snippet..

General

AI Verified

0

End php Session Error

Added: 1 year ago

Last Updated: 1 year ago

End php session error

General

AI Verified

1

PHP: Make Gutenberg video embeds responsive

Added: 9 months ago

Last Updated: 9 months ago

<p>Make sure video embeds like YouTube size responsively</p>

General

AI Verified

0

Really Simple Maintenance Mode

Added: 7 months ago

Last Updated: 7 months ago

Other Snippets in this Codevault

These are some popular snippets from this users codevault..

General

AI Verified

0

Delete WP Automatically Generated Images (not tested and audited yet)

Added: 7 months ago

Last Updated: 7 months ago

Delete from the database the images that WP automatically generated from uploaded images

General

AI Verified

0

Disable and Delete Image Sizes Generated by WordPress

Added: 7 months ago

Last Updated: 7 months ago

This WordPress code snippet optimizes image handling by disabling automatic generation of various image sizes during uploads and removing existing generated sizes, thereby enhancing site performance a...