5

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

enter image description here

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?

1
  • I thought it was the other way around and that you had to include custom sizes in that dropdown (using the global $_wp_additional_image_sizes). :::: Ain't no extra code somewhere in your set-up including the extra sizes?
    – brasofilo
    Commented Apr 10, 2013 at 20:25

1 Answer 1

6

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

4
  • My tests show that intermediate_image_sizes_advanced works but removes the upload, which I don't want. So I tested image_size_names_choose like this: pastebin.com/Gfngy1TQ and then uploaded a new image but the sizes are all visible in the drop-down. Is there a reason why image_size_names_choose doesn't work? Can you please double check that it works for you?
    – Christine Cooper
    Commented Apr 10, 2013 at 21:18
  • Ahh I found an instance when I use image_size_names_choose in my code, please view: pastebin.com/b0xD8DfP So that may be the reason why this is not working? You mentioned something about setting priorities to fix this? How would I go about doing that? Can you please demonstrate using my code...
    – Christine Cooper
    Commented Apr 10, 2013 at 21:27
  • Works like a charm! I understand now why there were such a confusion between us. Originally, I wrote that I want to disable image sizes from the drop-down list, when in fact, I wanted to hide them. I chose the wrong word for my question which lead me to be confused by you are proposing that I disable (remove) the sizes. :) So my sincere apology for the confusion and thank you for hanging in there. This is a great answer that will be helpful for others too.
    – Christine Cooper
    Commented Apr 10, 2013 at 22:51
  • 1
    No worries, it is pretty confusing, this post helped me remember how it works:)
    – Wyck
    Commented Apr 10, 2013 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.