Way to force media uploader use custom image size

When calling add_filter( 'image_size_names_choose', 'custom_image_sizes_choose' ); use your investigative skills to see how it would be best to call an if(thisweretrue) add_filter( 'image_size_names_choose', 'custom_image_sizes_choose' ); since I don’t know exactly what your specific situation is.

I actually was able to use your solution for setting up the custom sizes to help out with what I needed. In my scenario I had a custom meta box that I created and in that custom metabox I have a piece a javascript onclick that gets called which is as follows uploadimg_pt('mymetaboxfieldname');. The Javascript I used for getting the image from the media library is as follows:

function uploadimg_pt(pid) {
    window.send_to_editor = function(html) {
        imgurl = jQuery('img',html).attr('src');
        jQuery('#'+pid).val(imgurl);
        tb_remove();
    }
    formfield = jQuery('#'+pid).attr('name');
    tb_show('', 'media-upload.php?type=image&pid='+pid+'&TB_iframe=true');
    return false;
}

When using this javascript it opens up the media manager in a lightbox that generates an iframe. It then takes that pid in the argument of the function call and puts it in the URL when generating that iframe. In that iframe that gets generated it also will call the functions file in the template. So what I did was set an if statement in my functions file like this:

if($_GET['pid'] == "mymetaboxfieldname"){
    add_filter( 'image_size_names_choose', 'custom_image_size_large' );
}
function custom_image_size_large( $sizes ) {
    $custom_sizes = array(  
        'large' => 'Large'  
    );
    return $custom_sizes;  
}

Since I only needed the large size, that’s the one I returned.

I don’t know your exact scenario, but my hope is that this will help in figuring out what it is you need in yours.

Leave a Comment