Disable Automatic Creation of Additional Image Sizes in WordPress
Explanation
WordPress automatically creates several image sizes when you upload a picture. This can clutter your media library and take up unnecessary space. Here's how you can stop that from happening:
- Disable Default Image Sizes: The code removes the default sizes like thumbnail, medium, medium_large, and large. This means WordPress won't automatically create these versions when you upload an image.
- Remove Custom Image Sizes: If you've added custom sizes in your theme or plugins, this part of the code will remove them. You can add more
remove_image_size()
calls if you have additional custom sizes. - Disable Big Image Size Threshold: This stops WordPress from creating a scaled-down version of very large images, which it usually does to optimize loading times.
- Prevent Additional Image Sizes: By setting the width and height of the default sizes to zero, WordPress won't generate these extra sizes at all.
Using this approach, you can keep your media library neat and save server space by only keeping the original image size you upload.
Code
// Function to disable default WordPress image sizes
function wp_dudecom_disable_default_image_sizes( $sizes ) {
unset( $sizes['thumbnail'] );
unset( $sizes['medium'] );
unset( $sizes['medium_large'] );
unset( $sizes['large'] );
return $sizes;
}
add_filter( 'intermediate_image_sizes_advanced', 'wp_dudecom_disable_default_image_sizes' );
// Function to remove custom image sizes
function wp_dudecom_remove_custom_image_sizes() {
remove_image_size( 'custom-size-1' );
remove_image_size( 'custom-size-2' );
// Add more remove_image_size() calls if there are additional custom sizes
}
add_action( 'init', 'wp_dudecom_remove_custom_image_sizes' );
// Function to disable big image size threshold
add_filter( 'big_image_size_threshold', '__return_false' );
// Function to prevent WordPress from generating additional image sizes
function wp_dudecom_disable_additional_image_sizes() {
update_option( 'thumbnail_size_w', 0 );
update_option( 'thumbnail_size_h', 0 );
update_option( 'medium_size_w', 0 );
update_option( 'medium_size_h', 0 );
update_option( 'medium_large_size_w', 0 );
update_option( 'medium_large_size_h', 0 );
update_option( 'large_size_w', 0 );
update_option( 'large_size_h', 0 );
}
add_action( 'after_setup_theme', 'wp_dudecom_disable_additional_image_sizes' );
Instructions
To disable the automatic creation of additional image sizes in WordPress, follow these steps:
File Location: Add the code to your theme's functions.php
file or a custom plugin file.
Prerequisites: Ensure you have access to your WordPress theme files or the ability to create a custom plugin.
Implementation Steps:
- Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation.
- Locate the
functions.php
File: Navigate towp-content/themes/your-theme-name/functions.php
. - Edit the
functions.php
File: Open the file in a text editor. - Insert the Code: Copy and paste the provided code at the end of the
functions.php
file. - Save Changes: Save the file and upload it back to your server if using an FTP client.
- Verify: Upload a new image to your WordPress media library to ensure no additional image sizes are created.
By following these steps, you will prevent WordPress from automatically generating additional image sizes, keeping your media library organized and saving server space.
If you need further assistance or advanced functionality, consider reaching out to wp-dude.com for expert WordPress support.