Add Snippet To Project
Have you ever noticed a clutter of unnecessary image sizes while uploading media to your WordPress gallery? Unwanted image sizes take up server space, slow down your site, and can hamper your website’s overall performance. This article will lead you through the process of effectively removing unnecessary image sizes from your media upload gallery in WordPress, saving you both time and precious storage.
function wpturbo_remove_image_sizes( $sizes) {
unset( $sizes['thumbnail']);
unset( $sizes['medium']);
unset( $sizes['large']);
return $sizes;
}
add_filter('image_size_names_choose', 'wpturbo_remove_image_sizes');
The provided code allows you to remove default image size options from the WordPress media upload gallery.
You start by defining a new function named wpturbo_remove_image_sizes($sizes)
. This function will have the job of removing image sizes.
Let’s look closer at this function:
function wpturbo_remove_image_sizes( $sizes) {
unset( $sizes['thumbnail']);
unset( $sizes['medium']);
unset( $sizes['large']);
return $sizes;
}
When this function is executed, it’s given an array named $sizes
, which contains all the defined image size options. Each option in $sizes
is a key-value pair, with the key being the name of the size option, and the value being the label text displayed in the UI.
The unset()
function in PHP is used to destroy specified variables. In this case, it’s used three times within our function in order to unset or, effectively, remove the thumbnail, medium, and large sizes.
The keys thumbnail
, medium
, and large
correspond to the pre-defined WordPress image sizes. By unsetting these keys in the $sizes
array, these options will be removed from the media upload gallery where a user usually selects the desired image size.
Lastly, after unsetting these sizes, the modified $sizes
array is then returned.
After the function, you see this line:
add_filter('image_size_names_choose', 'wpturbo_remove_image_sizes');
Here, you use the add_filter
function to hook wpturbo_remove_image_sizes
into the image_size_names_choose
filter hook. WordPress uses this filter hook when preparing the list of available image size options. By adding this line, you’re instructing WordPress to apply your wpturbo_remove_image_sizes
function to this filter, effectively removing your specified size options the next time WordPress prepares this list.