How to Disable Unwanted Image Sizes From WordPress 5.3+

01/30/2024
Back to Gists
<?php
/**
 * Disable Unnecessary Generated Image Sizes
 * Author: Knol Aust
 * Gist Keywords: wordpress, media library, images, image sizes
 * Description: WordPress automatically generates multiple image sizes upon upload. This code disables unnecessary standard image sizes to reduce server/CDN bloat. Regenerate images after implementing.
 */

/**
 * Disable generated image sizes.
 *
 * @param array $sizes Current image sizes.
 * @return array Updated image sizes with unwanted sizes removed.
 */
function ka_disable_image_sizes($sizes) {
  
  // Remove or comment out any sizes you want to keep.
  // Keep in mind: some themes add additional sizes for specific purposes.
  // This function only removes standard WP 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']);    // Disable 2x medium-large size
  unset($sizes['2048x2048']);    // Disable 2x large size
  
  return $sizes;
}

// Hook to remove generated image sizes.
add_action('intermediate_image_sizes_advanced', 'ka_disable_image_sizes');

// Disable scaled image size.
add_filter('big_image_size_threshold', '__return_false');