AI Verified
Delete WP Automatically Generated Images (not tested and audited yet)
Delete from the database the images that WP automatically generated from uploaded images
PHP
Voted: 1 by 1 user(s)
Scroll down to see more snippets from this codevault.
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
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.
Last modified:
06/07/2024
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 : 10
Code : $path = pathinfo( $meta['file'] ));
Potential vulnerability found : File Inclusion / Path Traversal
Found on line : 27
Code : file( $intermediate_file )
Vulnerable line : 20
Code : $intermediate_file = $upload_dir['basedir'] . '/' . $path['dirname'] . '/' . $size_data['file']);
Found 2 vulnerabilities
1
7 months ago
I asked ChatGPT to come up with a fix to these vulnerabilities and here is the updated version: function delete_generated_image_sizes( $attachment_id ) { $meta = wp_get_attachment_metadata( $attachment_id ); $upload_dir = wp_upload_dir(); $path = pathinfo( $meta['file'] ); // Check if the dirname is outside the upload directory if (strpos($path['dirname'], $upload_dir['basedir']) !== 0) { return; } foreach( $meta['sizes'] as $size => $size_data ) { $intermediate_file = $upload_dir['basedir'] . '/' . $path['dirname'] . '/' . $size_data['file']; // Skip if the intermediate file is outside the upload directory if (strpos($intermediate_file, $upload_dir['basedir']) !== 0) { continue; } wp_delete_file( $intermediate_file ); } } add_action( 'delete_attachment', 'delete_generated_image_sizes' );
7 months ago
You can ignore my first comment because I updated the snippet above.
Please see some snippets below related to this snippet..
General
AI Verified
Added: 1 week ago
Last Updated: 1 week ago
Hides the user data from hackers using wp-json/wp/v2/users to find user info
General
Unverified
General
AI Verified
Added: 1 year ago
Last Updated: 4 months ago
Use this hook to remove all Google Fonts from website and use system fonts for best CLS Score.
These are some popular snippets from this users codevault..
General
AI Verified
Added: 7 months ago
Last Updated: 1 day ago
Delete from the database the images that WP automatically generated from uploaded images
General
AI Verified
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...
Category
Snippet Status