I have created a new image size using the add_image_size(), and I want to have it listed in the media library image size when I insert it. So I have the following code:
add_image_size( "grade-image", 320, 300, true );
function sgr_display_image_size_names_muploader( $sizes ) {
$new_sizes = array(
"0" => "grade-image",
);
/*
$added_sizes = get_intermediate_image_sizes();
// $added_sizes is an indexed array, therefore need to convert it
// to associative array, using $value for $key and $value
foreach( $added_sizes as $key => $value) {
$new_sizes[$value] = $value;
}
*/
// This preserves the labels in $sizes, and merges the two arrays
$new_sizes = array_merge( $new_sizes, $sizes );
return $new_sizes;
}
add_filter('image_size_names_choose', 'sgr_display_image_size_names_muploader', 11, 1);
however, it does not show the grade-image size. I tried the commented block to show a list of all image sizes, and it does show some of the other image sizes, but not the grade-image one. I'm really puzzled.
I have created a new image size using the add_image_size(), and I want to have it listed in the media library image size when I insert it. So I have the following code:
add_image_size( "grade-image", 320, 300, true );
function sgr_display_image_size_names_muploader( $sizes ) {
$new_sizes = array(
"0" => "grade-image",
);
/*
$added_sizes = get_intermediate_image_sizes();
// $added_sizes is an indexed array, therefore need to convert it
// to associative array, using $value for $key and $value
foreach( $added_sizes as $key => $value) {
$new_sizes[$value] = $value;
}
*/
// This preserves the labels in $sizes, and merges the two arrays
$new_sizes = array_merge( $new_sizes, $sizes );
return $new_sizes;
}
add_filter('image_size_names_choose', 'sgr_display_image_size_names_muploader', 11, 1);
however, it does not show the grade-image size. I tried the commented block to show a list of all image sizes, and it does show some of the other image sizes, but not the grade-image one. I'm really puzzled.
Share Improve this question asked Aug 13, 2013 at 2:12 AngelaAngela 4504 silver badges12 bronze badges3 Answers
Reset to default 7No, that seems to work right. You could test it by doing
<?php echo get_the_post_thumbnail('grade-image'); ?>
try this.
add_image_size( "grade-image", 320, 300, true );
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'grade-image' => __('Grade Image'),
) );
}
David's code will probably work but you should not use the add_image_size() function in that way. Check the example below :
add_action( 'after_setup_theme', 'my_custom_image_sizes' );
function my_custom_image_sizes() {
if ( function_exists( 'add_image_size' ) ) {
add_image_size( "grade-image", 320, 300, true );
}
}
add_filter( 'image_size_names_choose', 'my_custom_sizes' );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'grade-image' => __('Grade Image')
) );
}
Make sure you use the hook 'after_setup_theme' to avoid incompatibility errors.
add_image_size( "grade-image", 320, 300, true );
//required parameters 3 and 4
add_filter( 'image_size_names_choose', 'my_custom_sizes', 10, 1 );
function my_custom_sizes( $sizes ) {
return array_merge( $sizes, array(
'grade-image' => __('Grade Image'),
) );
}