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 have a few custom thumbnail sizes and obviously one of them is used in the set_post_thumbnail_size, what directly affects the interface in the admin area.

What I'm looking to do is to intercept which admin page (for post-type) I'm in and set a different size in the above function, so basically I'd like to have that:

function setup_the_theme() {

    add_theme_support( 'post-thumbnails' );

        if ($post_type == 'type_A'){
            set_post_thumbnail_size( 298, 167, true ); //portrait
        } else {
            set_post_thumbnail_size( 192, 108, true ); //landscape
        }

    add_image_size( 'top_articles', 298, 167, true ); //portrait
    add_image_size( 'article_thumb', 203, 114, true ); //portrait
    add_image_size( 'featured_articles', 60, 200, true ); //landscape

}
add_action( 'after_setup_theme', 'setup_the_theme' );

I suspect its too early in the cycle to know which post type i'm editing.

share|improve this question
    
Can you edit your question to describe your ultimate objective? What you're asking for seems like an implementation idea, rather than an end result. There very well may be a way to accomplish what you're after, without modifying set_post_thumbnail_size(). –  Chip Bennett Oct 20 '13 at 15:01
    
@ChipBennett pretty old question, i think i'd probably want a custom features image per post type in the admin side when editing a custom_post_type. maybe something like set_post_thumbnail_size(width, height, crop, post_type).. –  Amit Nov 20 '13 at 1:15
add comment

4 Answers

up vote 6 down vote accepted

Simpler way to figure out the post type of the item (which you are editing) and uploading media to:

$type = get_post_type($_REQUEST['post_id']);

As has been noted, the media upload iframe that is shown in the lightbox overlay when you try to upload something doesn't indicate the parent post that new upload will be attached to. But the $_REQUEST is still active from the edit window, and still contains the post_id of the item you're editing - so just query the type for that id...

You can use this technique in various hooks and filters - I've used the intermediate_image_sizes filter for image processing before, but not for this particular problem, but it should work there too...

share|improve this answer
1  
I don't think that works... The intermediate_image_sizes filter is called in async-upload.php, which isn't loaded with the $_REQUEST parameters... that's why I had to check them earlier and find somewhere to save the value in my example. –  goldenapples Dec 30 '10 at 5:46
    
Actually, you're right. It is there. Very slick... I checked for GET parameters before but didn't stop to think they might be POSTed. –  goldenapples Dec 30 '10 at 17:47
add comment

There is an intermediate_image_sizes filter in the function wp_generate_attachment_metadata. The problem is, it doesn't have the post type passed as part of the context. Here's one way of getting around that problem, by setting a transient with the value of the post type on the media upload popup:

add_action( 'admin_head-media-upload-popup', 'define_post_type' );

function define_post_type() {
    $referring_string = explode('?',$_SERVER['HTTP_REFERER']);
    if ($referring_string[1]) parse_str( $referring_string[1] );
    else $post_type = 'post';
    set_transient('current_upload_parent_type',$post_type);
}

add_filter( 'intermediate_image_sizes_advanced', 
     'set_thumbnail_size_by_post_type', 10);

function set_thumbnail_size_by_post_type( $sizes ) {

    $post_type = get_transient( 'current_upload_parent_type' );
    delete_transient( 'current_upload_parent_type' );

    switch ($post_type) :
        case 'type_A' :
            $sizes['post-thumbnail'] = array( 
                         'width' => 298, 'height' => 167, 'crop' => true);
            break;
        default :
            $sizes['post-thumbnail'] = array( 
                         'width' => 192, 'height' => 108, 'crop' => true);
            break;
    endswitch;

    return $sizes;

}

I think this should do the trick, although there probably is a more elegant way of doing this than setting a transient (and as it is, its not bulletproof - two simultaneous image uploads run the risk of getting their post types crossed, and images uploaded through the "New Media" panel may inherit the post type from the last image uploaded on the post screen.

Maybe someone can improve on this?

Edit:

I knew there had to be a better way of doing this. Ignore the function hooked on admin_head-media-upload-popup and follow @somatic 's answer for checking the post type. You can still set the post-thumbnail size based on post type in intermediate_image_sizes_advanced.

share|improve this answer
add comment

I'll repeat an earlier answer to a similar question:

There is no way to base the image sizes on the post type, as images are not yet attached to a post when you upload them and the intermediate image sizes are created (when you just add them via "New Media" they are not even attached to any post at all).

However, that answer contains info about plugins I wrote that together probably can do what you want: use multiple image sizes, but only create the images you actually use.

share|improve this answer
add comment

Just in case you don't know how to implement @somatic's excellent idea, here's how to do it.

The code'd go in your theme's functions.php:

if ( ! function_exists( 'wpse6103_setup' ) )
{
  function wpse6103_setup() {

    // Other theme defaults and register functions

    add_theme_support( 'post-thumbnails' );

      /*
       * Based on: http://wordpress.stackexchange.com/a/6123
       */
      if( ! empty( $_REQUEST[ 'post_id' ] ) && ctype_digit( $_REQUEST[ 'post_id' ] ) ) {

        /*
         * Check if post type is 'book'. If yes, set the thumbnail size to
         * 300 x 150. If not, obey the sizes defined for thumbnail in
         * WordPress Dashboard > Media > Image sizes > Thumbnail size.
         */
        if( get_post_type( 'book' == $_REQUEST[ 'post_id' ] ) ) {
          set_post_thumbnail_size( 300, 150, false );
        }

      }

  }
}
add_action( 'after_setup_theme', 'wpse6103_setup' );

Then to display featured image in your template, you can do something like this:

<?php

  if ( has_post_thumbnail() ) {

    if( 'aahank_book' == get_post_type() ) {

      echo wp_get_attachment_image( get_post_thumbnail_id( $post->ID ), array( 300, 150 ), false, array( 'class' => 'img-thumbnail' ) );

    } else {

      the_post_thumbnail( 'thumbnail', array( 'class' => 'img-thumbnail' ) );

    }

  }

?>

As I see it, you can't go with the usual the_post_thumbnail() function because it'll always display the image size that's set in WordPress Dashboard > Media > Image sizes > Thumbnail size and not the conditional image size we set for the custom post type earlier.

share|improve this answer
add comment

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.