Last active
August 1, 2019 19:57
-
-
Save filipecsweb/b9ca93d6e49c7a014797d57d1fadadf2 to your computer and use it in GitHub Desktop.
WordPress - Remove useless registered image sizes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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