Skip to content

Instantly share code, notes, and snippets.

@filipecsweb
Last active August 1, 2019 19:57
Show Gist options
  • Save filipecsweb/b9ca93d6e49c7a014797d57d1fadadf2 to your computer and use it in GitHub Desktop.
Save filipecsweb/b9ca93d6e49c7a014797d57d1fadadf2 to your computer and use it in GitHub Desktop.
WordPress - Remove useless registered image sizes
<?php
/**
* Hooked into `intermediate_image_sizes` filter hook.
*
* @param array $image_sizes
*
* @return array
*/
function ss_unset_intermediate_image_sizes( $image_sizes ) {
$bad_sizes = array(
array_search( 'medium_large', $image_sizes ), // Returns the key which contains the 'medium_large' value.
array_search( 'shop_catalog', $image_sizes ),
array_search( 'shop_single', $image_sizes ),
array_search( 'shop_thumbnail', $image_sizes ),
);
foreach ( $bad_sizes as $size_key ) {
if ( $size_key === false ) {
continue;
}
unset( $image_sizes[ $size_key ] );
}
/**
* Also, try to unregister additional image sizes.
*/
global $_wp_additional_image_sizes;
if ( isset( $_wp_additional_image_sizes ) && is_array( $_wp_additional_image_sizes ) ) {
if ( isset( $_wp_additional_image_sizes['shop_catalog'] ) ) {
unset( $_wp_additional_image_sizes['shop_catalog'] );
}
if ( isset( $_wp_additional_image_sizes['shop_single'] ) ) {
unset( $_wp_additional_image_sizes['shop_single'] );
}
if ( isset( $_wp_additional_image_sizes['shop_thumbnail'] ) ) {
unset( $_wp_additional_image_sizes['shop_thumbnail'] );
}
}
return $image_sizes;
}
/**
* @link https://developer.wordpress.org/reference/hooks/intermediate_image_sizes/
* @see ss_unset_intermediate_image_sizes()
*/
add_filter( 'intermediate_image_sizes', 'ss_unset_intermediate_image_sizes', 999 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment