1

I'm trying to set the srcset property of a block image. I'm replacing the image with another one and I noticed the attribute srcset is not being set, so I'm trying this with no success:

wp.data.dispatch('core/block-editor').updateBlock(blockId, {
    attributes: {
        id: imageData.id,
        url: imageData.url,
        srcset: imageData.srcset,
    },
});

Am I missing something?

The image gets displayed like this on post, without the srcset attribute for different resolutions:

<figure class="wp-block-image size-full"><img decoding="async" src="https://example.com/epa/wp-content/uploads/2023/06/demo-9.jpg" alt="" class="wp-image-3528"></figure>

The code editor code has no difference with normally upload images with srcset. So I assume there's nothing wrong on this part:

<!-- wp:image {"id":3546,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://example.com/epa/wp-content/uploads/2023/06/demo-9.jpg" alt="this is a demo" class="wp-image-3546"/></figure>
<!-- /wp:image -->
3
  • WordPress should automatically add srcset when the post is rendered. Is there a reason you're trying to do it yourself? Jun 1 at 12:39
  • Yeap, because I'm trying to replace an image block for another image I just upload programmatically.
    – Alvaro
    Jun 1 at 12:45
  • And somehow, WordPress won't create the srcset attribute once I modify the block with the new path and publish it.
    – Alvaro
    Jun 1 at 12:59

1 Answer 1

0

Ok, I found the issue.

After uploading the image programmatically I was calling wp_update_attachment_metadata to set the file metadata.

Apparently, that was creating this issue and srcset was not being set.

I have no idea why this is related though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.