How to Remove Unneeded Image Sizes in WordPress

WPTurbo » Snippets » How to Remove Unneeded Image Sizes in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you finding an excess of image sizes cluttering up your WordPress media library? For each image you upload, WordPress automatically creates several additional sizes. While this can be helpful in certain scenarios, it can also result in unnecessary wasted space if these sizes are not being used. In this article, we’ll guide you through the simple process of removing those unneeded image sizes in order to streamline your media folder and keep your website running smoothly.

					function wpturbo_remove_unneeded_image_sizes(){
    remove_image_size('thumbnail');
    remove_image_size('medium');
    remove_image_size('large');
}
add_action('init', 'wpturbo_remove_unneeded_image_sizes');
				

The provided code snippet is used to disable the automatic generation of default image sizes in WordPress. Whenever you upload an image, WordPress generates multiple scaled versions of the images for different purposes. These additional versions can lead to excessive storage consumption and may affect the performance of your website.

Now let’s break down each line of the code snippet and explain how it works.

First, we declare a new function named wpturbo_remove_unneeded_image_sizes(). The objective of this function is to remove the preset image sizes that WordPress automatically creates when we upload an image.

Within this function, we call the inbuilt WordPress function remove_image_size() multiple times, each time with a different parameter.

remove_image_size('thumbnail');
remove_image_size('medium');
remove_image_size('large');

These lines of code are directing WordPress to remove the ‘thumbnail’, ‘medium’, and ‘large’ image sizes. These are the default image sizes that WordPress creates every time you upload an image. By using the remove_image_size() function with the size names as parameters, we are instructing WordPress not to create these sizes when a new image is uploaded.

It’s important to note that this code does not delete or affect any existing images or their sizes; it only impacts new images from the point of the snippet being added. If you have a lot of already uploaded images in your media library in these sizes, you’d need to use an additional approach to clean those up.

Finally, the line add_action('init', 'wpturbo_remove_unneeded_image_sizes'); is where we are telling WordPress to run our function when it initializes. The ‘init’ is a hook that allows you to run a function after WordPress has finished loading but before any headers are sent.

Overall, this code helps in optimizing the storage and slightly improves the performance of your WordPress website by eliminating unnecessary image sizes. It’s a handy snippet, especially if you manage your own custom image sizes and do not need the WordPress defaults.

Register an account to save your snippets or go Pro to get more features.