Make WordPress Core

Opened 5 years ago

Closed 23 months ago

Last modified 11 months ago

#48003 closed defect (bug) (wontfix)

Function wp_prepare_attachment_for_js() missing other sizes

Reported by: kkatusic's profile kkatusic Owned by:
Milestone: Priority: normal
Severity: minor Version: 5.2.3
Component: Media Keywords: close
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Function wp_prepare_attachment_for_js() missing custom defined sizes attributes of images in returned array. After last up to WordPress on version 5.2.3. it missed custom sizes.

I tried on three different websites and it is same, custom sizes have been missing.

Change History (10)

#1 @kkatusic
5 years ago

Sorry I copy-pasted wrong function it is: wp_prepare_attachment_for_js()

#2 @SergeyBiryukov
5 years ago

  • Description modified (diff)
  • Summary changed from Function get_intermediate_image_sizes() missing other sizes to Function wp_prepare_attachment_for_js() missing other sizes

#3 @iCaleb
5 years ago

Hmm, I'm struggling to replicate any differences with recent versions. Do you recall which version of WordPress you were on previously?

According to [the code](https://github.com/WordPress/WordPress/blob/ecf00fc84407249ae2a699e3b3d31571194efa34/wp-includes/media.php#L3462-L3478), everything works correctly for me. For example, if I have the following in my theme:

add_filter( 'image_size_names_choose', 'custom_image_size_names_48003' );
function custom_image_size_names_48003( $sizes ) {
  return array_merge( $sizes, array(
    'my-custom-size' => __( 'My Custom Size' ),
  ) );
}

add_action( 'after_setup_theme', 'add_custom_image_sizes_48003' );
function add_custom_image_sizes_48003() {
  add_image_size( 'my-custom-size', 220, 180, true );
  add_image_size( 'some-other-size', 300 );
}

Then wp_prepare_attachment_for_js() for a newly uploaded image will return 'sizes' => array(4) { 'thumbnail', 'medium', 'vip-custom-size', 'full' }.

Note that only the custom image size that I gave a display name shows up. More info: https://developer.wordpress.org/reference/functions/add_image_size/#for-media-library-images-admin

Version 0, edited 5 years ago by iCaleb (next)

#4 @kkatusic
5 years ago

I created test page, you can check it here: http://bit.ly/2LPa522 . You can see it printed only main data sizes that WP provided, but I have here some custom image sizes, to be precise I have 11 custom sizes.

Only difference between my code and your is that I don't use hook

image_size_names_choose

#5 @iCaleb
5 years ago

Only difference between my code and your is that I don't use hook image_size_names_choose

Right, that is the key here. Without "registering" the custom image size with a display name, it won't be shown on the attachment page in the dropdown, thus wp_prepare_attachment_for_js() doesn't need the data.

You are sure this behavior was previously different? And if so, what version of WP were you on before you updated?

#6 @kkatusic
5 years ago

I don't remember, before last update of WP it's been worked without any disadvantages.

If you think that code is working like it should be, you can close ticket.

#7 @iCaleb
5 years ago

I'll wait for a second opinion to close this, just to make sure I'm not missing something.

To resolve your particular problem though, I'd recommend either using the image_size_names_choose filter to register the custom image sizes you need this function to return. Or modify $response['sizes'] in the output with the wp_prepare_attachment_for_js filter: https://github.com/WordPress/WordPress/blob/ecf00fc84407249ae2a699e3b3d31571194efa34/wp-includes/media.php#L3585

#8 @kkatusic
5 years ago

Ok, thx.

#9 @Mista-Flo
23 months ago

  • Keywords close added; needs-patch removed
  • Resolution set to wontfix
  • Status changed from new to closed

#10 @swissspidy
11 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.