image_size_names_choose filter doesn’t show

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.

Related posts

3 comments

  1. 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'),
        ) );
    }
    
  2. 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.

  3. 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'),
        ) );
    }
    

Comments are closed.