WP_Image_Editor – How to save the new size of the image in the sizes metadata and create custom srcset


For a custom post type I have an image upload/media library field, that adds images as attachments in the post.

While saving the post I am resizing the selected image – to some new non existing images sizes with WP_Image_Editor.

This is the part of the code, when I generate the new image sizes:

$path = wp_get_original_image_path( $imageObject['ID'] );

$wp_editor = wp_get_image_editor( $path, array() );

$result = $wp_editor->resize($size['max_w'], $size['max_h'], $crop);

if( !is_wp_error( $result ) ) {

  $wp_editor->save( $wp_editor->generate_filename() );

So, I am saving the new sizes, next to the original and the existing ones.

I am able to load one of those new image sizes using:
wp_get_attachment_image_src($imageObject['ID'], [$size['max_w'], $size['max_h']]);

But, I would like to be able to find the new sizes in the sizes element of the image array when I call wp_get_attachment_image($imageObject['ID']).

Ideally, I would like to be able to produce a new responsive image that would include those new sizes in the srcset.

Is this possible? Am I missing something? –probably.

Do I have to save them with a different way – or do I have to add any additional steps before or after saving, in order to add the new sizes to the image?

And what about naming those new sizes?

FFrewin 1 year 2022-12-13T05:28:06-05:00 0 Answers 0 views 0

Leave an answer