Skip to content

Instantly share code, notes, and snippets.

@tdmrhn
Created December 27, 2022 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdmrhn/48d89c09605be80e4031bdfabf05d57f to your computer and use it in GitHub Desktop.
Save tdmrhn/48d89c09605be80e4031bdfabf05d57f to your computer and use it in GitHub Desktop.
Wordpress remove extra image sizes
<?php
add_filter( 'intermediate_image_sizes', function ( $sizes ) {
// keep only 'thumbnail', 'medium', 'large'
$targets = ['medium_large', '1536x1536', '2048x2048','shop_thumbnail', 'shop_single', 'shop_catalog', 'woocommerce_thumbnail', 'woocommerce_single', 'woocommerce_gallery_thumbnail'];
foreach($sizes as $size_index=>$size) {
if(in_array($size, $targets)) {
unset($sizes[$size_index]);
}
}
return $sizes;
}, 10, 1);
@tdmrhn
Copy link
Author

tdmrhn commented Mar 26, 2024

Vice versa, i prefer this.

add_filter( 'intermediate_image_sizes', function ( $sizes ) {
  $allowed_sizes = ['thumbnail', 'medium'];
  $sizes = array_intersect($sizes, $allowed_sizes);
  return $sizes;
}, 10, 1);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment