I have three different custom post types which require an image upload in the admin post edit page.

Each type has its own image size setting. Let's say:

  • 'news' type => picture size required: 700x320px
  • 'work' type => picture size required: 700x430px
  • 'press' type => picture size required: 460x600px

I succeeded in removing the default image size settings and in adding these 3 new ones in the Media Uploader but I don't need them to be displayed as radio buttons at all as I don't want the users to select anything regarding the sizes when uploading pictures.

What I would like instead is to get the Media Uploader to set the accurate image size setting right away according to the custom type of the actual post, with no user intervention whatsoever.

Is this possible?

I tried to query the post_type from within the Media Uploader Iframe but it gives me at best the "attachment" post type (which makes sense as it is the post type of the picture it's trying to upload) while I try to get the custom type of the post I'm about to publish.

And if I can't get the custom type of the post, I can't see how I can get the Media Uploader to set the size accordingly.

I just don't know how to make it work. Any help would be appreciated.

Here is my script so far:

function media_image_delete_sizes($sizes) 

    return $sizes;
add_filter('image_size_names_choose', 'media_image_delete_sizes');

add_image_size( 'news-size' , 700, 320,true);
add_image_size( 'work-size' , 700, 430,true);
add_image_size( 'press-size', 460, 600,true);

function media_image_add_sizes($sizes) 
    // I would like not to show any of these new image sizes 
    // (I will probably hide them via css or jQuery)    
    $addsizes = array(
        "news-size"  => "News  (700x320)",
        "work-size"  => "Works (700x430)",
        "press-size" => "Press (460x600)"
    $newsizes = array_merge($sizes, $addsizes);

    return $newsizes;
add_filter('image_size_names_choose', 'media_image_add_sizes');

function theme_default_image_size() 
    //This variable will retrieve the post_type of the picture post, not the one of the 
    //underlying CPT post
    global $post_type;

    // Here is the place where I tried to place the conditional return
    // but I just can't succeed in retrieving the CPT post_type as I always get "attachment".

    if     ($post_type == "news" ){ return 'news-size';}
    elseif ($post_type == "work" ){ return 'work-size';}
    elseif ($post_type == "press"){ return 'press-size';}
    else                          { return 'this-doesnt-work';};
add_filter( 'pre_option_image_default_size', 'theme_default_image_size' );

Thank you.

1 Answer 1


Add following code in your themes functions.php file

function theme_set_default_image_size() {
global $post_type;
$custom_image_size = 'medium';   

if($post_type == 'news')
    $custom_image_size = 'news_image_size';
else if($post_type == 'product')
    $custom_image_size = 'product_image_size';
else if($post_type == 'service')
    $custom_image_size = 'service_image_size';

return $custom_image_size;
add_filter( 'pre_option_image_default_size', 'theme_set_default_image_size' );
  • Thank you Vinod Dalvi. I already use this exact same script but it will not set any custom image size, whatever the post because the acutual custom type is never part of the condition. As I said before, this is probably due to the fact that the Media Uploader is inside an Iframe and that the current post is the one of the media file (different from the article one) and that the post_type from that window is always "attachment" (which is the custom post type of the picture it is dealing with). That's the core of the problem. I think it's a scope problem.
    – Baylock
    Feb 28, 2013 at 17:47
  • I might add that the Media Uploader must know the CPT post attributes somehow as it uses them in the database to link the attachment post id to the CPT post id, but the global variables regarding the post/post_id/custom_post_type retrieved when queried are those of the attachment post, not those of the CPT post. I checked this by testing the custom post type queried when the Media Uploader pops up and its always "attachment"...
    – Baylock
    Feb 28, 2013 at 18:01

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.