Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ThemeMetric/0172a7863b560eb8d645 to your computer and use it in GitHub Desktop.
Save ThemeMetric/0172a7863b560eb8d645 to your computer and use it in GitHub Desktop.
<?php
add_action( 'pre-upload-ui', 'get_the_post_type' );
function get_the_post_type() {
$post_type = isset( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : 'post';
set_transient( 'attached_post_type', $post_type );
}
add_filter( 'intermediate_image_sizes_advanced', 'add_image_size_for_post_type', 10 );
function add_image_size_for_post_type( $sizes ) {
$post_type = get_transient( 'attached_post_type' );
delete_transient( 'attached_post_type' );
/*
* BLOCK A
*
* This can be used if you want to set different size for only one post type. For multiple post type, check BLOCK B
*
*/
if( $post_type == 'page' ){
$sizes['post-thumbnail'] = array( 'width' => 275, 'height' => 175, 'crop' => true );
}
// END OF BLOCK A
/*
* BLOCK B
*
* This can be used if you want to set different sizes for different post types.
*
*/
$post_type_size = array(
'post' => array( 'width' => 275, 'height' => 175, 'crop' => true ),
'page' => array( 'width' => 375, 'height' => 475, 'crop' => true ),
'CPT' => array( 'width' => 475, 'height' => 675, 'crop' => true )
);
if( isset( $post_type_size[$post_type] ) ) $sizes['post-thumbnail'] = $post_type_size[$post_type];
// END OF BLOCK B
return $sizes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment