Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've implemented media uploader to my custom category image plugin and was wondering can I somehow force it use my custom image size I've set only for categories.

enter image description here

... So in screen shot above, I'd like Size column only contain "Category Image (80 x 80)" and automatically be selected.

The way I've added that 'Category Image' to the list is shown below

add_filter( 'image_size_names_choose', 'custom_image_sizes_choose' );

function custom_image_sizes_choose( $sizes ) {  
    $custom_sizes = array(  
        'category-image' => 'Category Image'  
    );
    return array_merge( $sizes, $custom_sizes );  
}

If in that function I only return $custom_sizes it will affect every media uploader context, eg. Posts->Add Media, and not only when setting up my category image.

Here's how I've implemented Media Uploader in my plugin

add_action( 'admin_init', 'cat_image_options_setup' );

//------------------------------------------------
// Setup category image media picker
//------------------------------------------------
function cat_image_options_setup() {
    global $pagenow;
    if ( 'media-upload.php' == $pagenow || 'async-upload.php' == $pagenow ) {
        // Now we'll replace the 'Insert into Post Button inside Thickbox' 
        add_filter( 'gettext', 'replace_thickbox_text' , 1, 2 );
    }
}

//------------------------------------------------
// Replace the media picker button text
//------------------------------------------------
function replace_thickbox_text( $translated_text, $text ) { 
    if ( 'Insert into Post' == $text ) {
        $referer = strpos( wp_get_referer(), 'cat_image_settings' );
        if ( $referer != '' ) {
            return __( 'Add as a category image', 'ddr' );
        }
    }

    return $translated_text;
}

Any way to achieve that? Thanks!

PS. If someone knows better way to implement that media uploader, please let me know. Now it somehow uses the old media uploader window, and not the same as in Posts->Add Media.

share|improve this question

1 Answer 1

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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