WordPress Snippets at WPcustoms

Create new image sizes for custom post types

By default WordPress creates three images sizes for every picture uploaded to your media library. If you want to create a new size for custom post types you can copy this code to your functions file. So instead of using “thumbnail” you can use “your_image_size_1” or whatever you decide. Make sure to set values for your new size with the add_image_size function


/**
 * Snippet Name: Create new image sizes for custom post types
 * Snippet URL: http://www.wpcustoms.net/snippets/create-new-image-sizes-for-custom-post-types/
 */
 add_filter( 'intermediate_image_sizes', 'ravs_slider_image_sizes', 999 );
function ravs_slider_image_sizes( $image_sizes ){

    // size for slider
    $slider_image_sizes = array( 'your_image_size_1', 'your_image_size_2' ); 

    // for ex: $slider_image_sizes = array( 'thumbnail', 'medium' );

    // instead of unset sizes, return your custom size for slider image
    if( isset($_REQUEST['post_id']) && 'your_custom_post_type' === get_post_type( $_REQUEST['post_id'] ) )
        return $slider_image_sizes;

    return $image_sizes;
}


// to create a custom size you can use this:
   add_image_size('your_image_size_1',100,100,true); // Crop mode
   add_image_size('your_image_size_2',400,500); // Resize mode