5

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.

3 Answers 3

7

No, 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'),
    ) );
}
4
  • I don't know why, but that didn't work either. Perhaps my add_image_size is wrong somehow?
    – Angela
    Commented Aug 13, 2013 at 3:07
  • 6
    Try wordpress.org/plugins/regenerate-thumbnails Pretty sure the size isnt picked up till you upload a new image or regenerate your old ones. Commented Aug 13, 2013 at 3:16
  • 2
    David's code works fine, maybe you have a problem with a theme or plugin. It will only effect new uploaded images though.
    – Wyck
    Commented Aug 13, 2013 at 3:32
  • Thanks @DavidHechler, it was the regeneration that's the issue.
    – Angela
    Commented Aug 13, 2013 at 3:58
1

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.

-1
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'),
    ) );
}
1
  • 1
    Hi, could you elaborate a little bit why do you think this is a correct answer and is this a solution for that problem - it'll make it easier to learn something from your answer :) Commented Apr 17, 2019 at 11:05

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.