• Let’s talk about your project
  • contact@ohm.solutions

How to Remove Additional Image Sizes in WordPress

February 20, 2020
February 20, 2020 Nico Lauria

By default WordPress will generate additional image sizes each time you add an image. Ultimately your theme decides which sized images to use for different modules. WordPress will generate a small, medium, medium-large, and large image for each photo that you add. Additionally the theme you are using may add more image sizes. As your website grows and you continue adding images they can start to take up a large amount of space and also make it harder to navigate your uploads folder. There are two steps for disabling an image size in WordPress.

Determining Image Sizes

First we need to figure out in total what image sizes are being generated. We can do that with the code below. It will print the results of our $_wp_additional_image_sizes global object to a file called ‘image-sizes.txt‘ in the root of our WordPress installation.

Here is the code that should be placed at the bottom of your functions.php file:

add_action( 'admin_init', '_get_all_image_sizes' );

function _get_all_image_sizes() {
    global $_wp_additional_image_sizes;

    $default_image_sizes = get_intermediate_image_sizes();

    foreach ( $default_image_sizes as $size ) {
        $image_sizes[ $size ][ 'width' ] = intval( get_option( "{$size}_size_w" ) );
        $image_sizes[ $size ][ 'height' ] = intval( get_option( "{$size}_size_h" ) );
        $image_sizes[ $size ][ 'crop' ] = get_option( "{$size}_crop" ) ? get_option( "{$size}_crop" ) : false;
    }

    if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) {
        $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
    }

    $file_path = get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'image-sizes.txt';
    file_put_contents($file_path, print_r($image_sizes, TRUE));
}

Once you have added the code below and refreshed your site your ‘image-sizes.txt’ file should be created. It will look something like this:

Printing WordPress Image Sizes to File

This file should have all of your image sizes. Another way of finding the image size is to go to your uploads folder and look at all of the image sizes being created for an image.

Determine which image sizes you want to disable. Each theme will have its own image sizes. This is something you will have to determine by loading the site in your browser and looking at the image sizes that are being served. You can right click on the page and click ‘inspect element’. Then go to the Network tab and click on ‘img’. It will show the name of the images that are loaded on the page.

Network Tab Showing Images Loaded

Disabling the image sizes

Now that we know our image sizes. We can disable them with the code below added to our functions.php file. Each ‘unset’ line targets one of the sizes from our ‘image-sizes.txt’ file.

add_action('intermediate_image_sizes_advanced', 'disable_additonal_image_sizes');

function disable_additional_image_sizes($sizes) {
  unset($sizes['thumbnail']);    // disable thumbnail size
  unset($sizes['medium']);       // disable medium size
  unset($sizes['large']);        // disable large size
  unset($sizes['medium_large']); // disable medium-large size
  unset($sizes['1536x1536']);
  unset($sizes['2048x2048']);
	
  return $sizes;
}

As always we would love to hear back from your with questions or feedback. Feel free to leave a comment or reach out to us directly.

Contact

Get Connected.

We welcome you to contact us for more information
about any of our products or services.

Let’s Talk About Your Project

We’d love to hear about your project and your ideas. Give us a call or send us an email to set up a time to speak.

Connect With Us On Social Media

Find OHM on one of our social media accounts below and let us know what you think about our work.

Set Up A Free Consultation

Ready to get started? Tell us some details about your project using our questionnaire form.Consultation

CALL: 916-335-8159

1029 H Street Suite 306
Sacramento, CA 95814

Contact