Hide custom image sizes from media library

I want to hide a few custom image sizes from the media uploader:

enter image description here

Read More

The following code (posted here) works only for the default image sizes:

function remove_image_sizes($sizes) {
    unset($sizes['image-name']);
    return $sizes;
}
add_filter('image_size_names_choose', 'remove_image_sizes');

Is there a similar solution to unset custom image sizes?

Related posts

Leave a Reply

1 comment

  1. Using unset and intermediate_image_sizes_advanced will work but only on images uploaded after the function is added. To change it for existing images you need to regenerate them using a plugin ( in essence deleting that image size) or just hide that option from being visible.

    Tested on 3.5.1

    // add custom image size
    function mytheme_95344() {
            add_image_size('x-la',800,800, false);
    }
    add_action( 'after_setup_theme', 'mytheme_95344' );
    
    // remove it
    function remove_image_size_95344($sizes) {
        unset($sizes['x-la']);
        return $sizes;
    }
    add_filter('intermediate_image_sizes_advanced', 'remove_image_size_95344');
    

    So this x-la size will still show for images before the unset function was added.

    To remove this you can.

    1. Regenerate all images (a pain).
    2. Hide it from the display using image_size_names_choose

      function remove_image_size_95344($possible_sizes) {
      
       unset( $possible_sizes['x-la'] );
       return $possible_sizes;
      
      }  
      add_filter('image_size_names_choose', 'remove_image_size_95344');
      

    To clarify the filters:

    intermediate_image_sizes_advanced – effects the actual upload
    image_size_names_choose – effects the visibility of the dropdown in the media box

    Also note:

    It could be that a theme or plugin is already using the image_size_names_choose filter somewhere since it is very common when using add_image_size for custom images. If that is the case you can still use a second image_size_names_choose filter and set the $priority to fire after the first one.

     //default filter adding your custom sizes to dropdown
     // not having a 3rd parameter will default to priority of 10
     add_filter( 'image_size_names_choose', 'my_insert_custom_image_sizes' );
     //second use of same filter to remove images note the additional 15 to fire after the above
     add_filter( 'image_size_names_choose', 'remove_image_sizes', 15);
    

    To view how filters work: http://codex.wordpress.org/Function_Reference/add_filter