Skip to content

Conversation

@ramonjd
Copy link
Member

@ramonjd ramonjd commented Nov 20, 2025

What? How?

Found while testing #73157

Initialize $img_srcset to false before the conditional block, matching the pattern used for other variables and the return value of wp_get_attachment_image_srcset() when no attachment ID is available.

Why?

When "Enlarge on click" (lightbox) is enabled via global styles, a PHP warning occurs for image blocks in post lists (e.g., on the home page's query block).

Warning: Undefined variable $img_srcset in /var/www/html/wp-content/plugins/gutenberg/build/scripts/block-library/image.php on line 224

In block_core_image_render_lightbox(), $img_srcset is only set inside if ( isset( $block['attrs']['id'] ) ), but it's used later in wp_interactivity_state() regardless of whether the condition was met.

Unlike $img_width and $img_height, which are initialized to 'none' before the conditional, $img_srcset wasn't initialized.

Testing Instructions

Setup:

  1. Create a new post/page
  2. Insert an Image block and save.

Enable the lightbox in global styles:

  1. Go to Appearance → Editor → Styles
  2. Navigate to Blocks → Image
  3. Enable "Enlarge on click" in global settings
  4. Save changes

Visit the homepage or a template with a posts query block.

Ensure the following error is not logged:
Screenshot 2025-11-20 at 2 01 23 pm

@ramonjd ramonjd self-assigned this Nov 20, 2025
@ramonjd ramonjd requested a review from ajitbohra as a code owner November 20, 2025 03:02
@ramonjd ramonjd added [Type] Bug An existing feature does not function as intended [Block] Image Affects the Image Block labels Nov 20, 2025
@ramonjd ramonjd requested a review from fabiankaegy as a code owner November 20, 2025 03:02
Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

I could reproduce by testing an Image block using "Insert from URL", too, where no id gets set.

Looks like the issue was introduced when #61107 landed, and I see from that PR that the $img_srcset value winds up being handled via lightboxSrcset in getImageSrcset here where falsy values will be treated as an empty string. So code-wise it's looking good, and in practice this is testing well for me with the lightbox working as expected on the site frontend.

LGTM 👍

@github-actions
Copy link

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ramonjd ramonjd merged commit 0186ae6 into trunk Nov 20, 2025
41 of 42 checks passed
@ramonjd ramonjd deleted the fix/image-undefined-srcset branch November 20, 2025 05:15
@github-actions github-actions bot added this to the Gutenberg 22.2 milestone Nov 20, 2025
@t-hamano
Copy link
Contributor

I think this warning is new in 6.9. Is it worth backporting to 6.9 RC3?

cc @Takshil-Kunadia @westonruter @luisherranz

@westonruter
Copy link
Member

@t-hamano This issue appears to have been introduced as part of #61107 and released with Gutenberg 22.1. Is that included in 6.9?

@t-hamano
Copy link
Contributor

@westonruter Sorry, I was mistaken. Gutenberg 22.1 is NOT to be included in WordPress 6.9, so there is no need to backport this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Image Affects the Image Block [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants