Make WordPress Core

Opened 17 months ago

Last modified 2 weeks ago

#63577 new defect (bug)

PHP Warning from `get_post_galleries()` when gallery items lack an ID.

Reported by: dd32 Owned by:
Priority: normal Milestone: Future Release
Component: Gallery Version:
Severity: normal Keywords: has-patch has-unit-tests needs-test-info changes-requested
Cc: Focuses:

Description

As implemented in #43826 a PHP warning can be thrown pretty consistently with certain block contents

E_WARNING: Undefined array key "id" in wp-includes/class-wp-list-util.php:170

This warning can be silenced via #59774, but it's specifically from get_post_galleries() due to the input that's being passed, as seen in the stack trace below:

get_post_galleries( $post = 121142, $html = FALSE )
wp_list_pluck( $input_list = [0 => ['width' => '188px', 'height' => 'auto'], 1 => ['width' => '251px', 'height' => 'auto']], $field = 'id', $index_key = ??? )
WP_List_Util->pluck( $field = 'id', $index_key = NULL )

It seems that we can likely just fix this invalid input to wp_list_pluck() rather than avoiding the warnings in that function specifically.

Given the code in question is fairly simple, we could just loop over the attributes and remove the need for wp_list_pluck() entirely. See attached PR.

Change History (18)

This ticket was mentioned in ​PR #8992 on ​WordPress/wordpress-develop by ​@dd32.


17 months ago
#1

  • Keywords has-patch added

​@dd32 commented on ​PR #8992:


17 months ago
#2

I should add.. I didn't test this..

#3 @swissspidy
17 months ago

  • Keywords needs-unit-tests added
  • Milestone Awaiting Review → 6.9

#4 @rollybueno
17 months ago

  • Keywords has-test-info added

Reproduction Report

Description

This report confirms that malformed or incomplete [gallery] shortcodes, such as [gallery], [gallery ids], or [gallery ids=""] can cause get_post_galleries() to return arrays without an 'id' key. Passing this result into wp_list_pluck() triggers a PHP warning due to the missing array key.

Environment

  • WordPress: 6.8.1
  • PHP: 8.3.17
  • Server: nginx/1.26.1
  • Database: mysqli (Server: 8.0.16 / Client: mysqlnd 8.3.17)
  • Browser: Chrome 136.0.0.0
  • OS: Linux
  • Theme: Twenty Twenty-Five 1.2
  • MU Plugins: None activated
  • Plugins:
    • Query Monitor 3.18.0
    • Test Reports 1.2.0

Steps to Reproduce

  1. Go to Posts > Add New.
  2. Open the Code Editor in the block editor.
  3. Paste one of the following shortcode using Shortcode block (any of these will trigger the issue):
[gallery]

or

[gallery ids]

or

[gallery ids=""]
  1. Publish the post.
  2. Add the following debug snippet in theme’s functions.php to trigger it:
add_action( 'wp_footer', function () {
    if ( ! is_singular() ) return;

    $galleries = get_post_galleries( get_the_ID(), false );

    // Will trigger a PHP warning
    $ids = wp_list_pluck( $galleries, 'id' );

    echo '<pre>';
    print_r( $galleries );
    echo '</pre>';
});
  1. Visit the post on the frontend.

Actual Results

✅ PHP Warning appears in the debug log:
https://i.imgur.com/QPPBLob.png

A corresponding stack trace will point to the wp_list_pluck() line.

Additional Notes

  • The issue does not require valid image IDs. Any malformed or empty [gallery] shortcode will trigger the issue.
  • This originates from get_post_galleries() returning gallery data without the 'id' key.
  • A defensive code change (e.g. fallback or key check before plucking) is advisable.
  • Warning suppression (like #59774) won't solve the underlying data inconsistency.

Supplemental Artifacts

Debug stacktrace:
https://i.imgur.com/XIZDX00.png

This ticket was mentioned in ​PR #9005 on ​WordPress/wordpress-develop by ​@rollybueno.


17 months ago
#5

  • Keywords has-unit-tests added; needs-unit-tests removed

## Summary

This PR adds a PHPUnit test that reproduces the issue reported in Trac #63577, where a warning is triggered when get_post_galleries() encounters malformed [gallery] shortcodes.

Specifically, the issue occurs when the gallery array lacks the expected 'id' key, and functions like wp_list_pluck() assume it exists. This test demonstrates how [gallery], [gallery ids], or [gallery ids=""] can result in a malformed output that reproduces the warning.

## Testing Instructions

Run the test class:

npm run test:php -- --filter getPostGalleries

Trac ticket: https://core.trac.wordpress.org/ticket/63577

#6 @rollybueno
17 months ago

  • Keywords needs-testing added

#7 @SirLouen
17 months ago

  • Keywords needs-test-info added; has-test-info needs-testing removed

@dd32 how can you make that the gallery items have no attached IDs?

I'm unable to reproduce this just by calling to get_post_galleries($post_id, false); as you suggest.
I'm not confident that the reproduction report of rolly is what you mean, afaik, thereis not id but ids

https://i.imgur.com/ixkOdLm.png

I understand that you can trigger this when ids happens to be empty and the function is trying to pluck an empty string throwing such error. But I have not found a way to create a gallery that has an empty ids string.

If I add a gallery without images, this is the result
https://i.imgur.com/z45bvB5.png

and no array at all
https://i.imgur.com/3ZNMGK7.png

If you are asking at this moment why I have put screenshots instead the text? Only god knows, I was thinking this right now, but it's too late now for me to rollback

Version 0, edited 17 months ago by SirLouen (next)

#8 @dd32
17 months ago

@SirLouen I'm not sure :) But I made an assumption that it would be from old galleries converted to blocks OR that galleries could have arbitrary images in it.

I was able to duplicate it with this post on WordPress.org:
​https://make.wordpress.org/training/2025/06/12/wordcamp-europe-2025-contributor-day-recap/

Which has two galleries, one with attachments, and one with images that are embedded from a Google Doc (which is a different problem...).

It seems that the Gallery block doesn't allow inserting images by URL (Only upload or from Media library) but it does seem to allow either pasting images which have an offsite URL or for an image to be grouped into a gallery when it's not a hosted image..

<!-- wp:gallery {"linkTo":"file"} -->
<figure class="wp-block-gallery has-nested-images columns-default is-cropped">
<!-- wp:image {"width":"188px","height":"auto"} -->
<figure class="wp-block-image is-resized"><img src="https://lh7-us.googleusercontent.com/SZ0uE-B_Jz4GXspCylPqTywrpQKjjLFdtkGdP_-5nRakAwm1D3gtmbWhTSzOGIUhSdFrg5N4aGRR__MTz0qq1UQHgBfABPw4mG8RXvv5nDvHCXV9xJv500kRycJwlP3-x3hK_rixNX3y-Uf5QiA-eDc" alt="" style="width:188px;height:auto" /></figure>
<!-- /wp:image -->

<!-- wp:image {"width":"251px","height":"auto"} -->
<figure class="wp-block-image is-resized"><img src="https://lh7-us.googleusercontent.com/-XTajDctcb-dNHbgt5HRe7ANRal2UlzeQIXjdVdw1iOU1K7pjbXFnUgpHv34dZvOOxC7L1bQOF_nZaLF94eYTRj9cAeQ96I_i9AiMc9IaPV_-rbvnrOU7oiQb_niNAVg1jN3-q1yX8HF-uFOCb0EySg" alt="" style="width:251px;height:auto" /></figure>
<!-- /wp:image --></figure>
<!-- /wp:gallery -->

#9 @wildworks
12 months ago

  • Keywords changes-requested added

To move this ticket forward, can we do the following?

  • Combine the two pull requests (​PR 8992, ​PR 9005) into one and rebase it on top of the latest trunk.
  • Add more unit tests. We'll probably need to add a data provider and bind the data to the post content ​here.

#10 @wildworks
12 months ago

Since the 6.9 RC1 release is coming soon, I will punt this ticket to 7.0.

#11 @wildworks
12 months ago

  • Milestone 6.9 → 7.0

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


9 months ago

#13 @audrasjb
9 months ago

From WP 7.0 bug scrub:
The latest comment still applies if you're still following this ticket @rollybueno:

  • It would be nice to merge the 2 PR into one
  • Unit Tests: adding a data provider for an extended set of test would be good

This ticket was mentioned in ​Slack in #core by audrasjb. ​View the logs.


8 months ago

#15 @audrasjb
8 months ago

  • Milestone 7.0 → Future Release

As per today's 7.0 pre-RC1 bug scrub:
This is not ready to ship, let's move it to Future Release.

This ticket was mentioned in ​PR #14250 on ​WordPress/wordpress-develop by ​@irozum.


4 weeks ago
#16

Fixes the Undefined array key "id" warning thrown by get_post_galleries() when a Gallery block contains inner image blocks without an id attribute (e.g. images inserted by URL or converted from other content). Rather than silencing the warning in wp_list_pluck(), the function now loops over the inner blocks and only collects IDs that exist, as suggested on the ticket.

This supersedes the two stale PRs on the ticket (#8992 for the fix, #9005 for the test) by combining them into one, with a data provider covering no inner block IDs, partial IDs, and all IDs, as requested by committers in the ticket comments.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Implementation, tests, and PR description. Reviewed by Igor Rozum.

---

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See ​GitHub Pull Requests for Code Review in the Core Handbook for more details.

​@abhishekfdd commented on ​PR #14250:


2 weeks ago
#17

Tested this on top of current trunk, full report is on the Trac ticket. Fixes the warning with no change in output for the normal block and shortcode cases. A few small things:

  1. The new data provider should be public static function. The existing provider in this file is static, and non-static providers are deprecated in newer PHPUnit. Fine to fix on commit if easier.
  1. An image block with "id": "" still produces a stray comma in ids (e.g. ,6), since the loop only checks isset(). Trunk behaves the same, so it's not a regression, but since the point of the change is to only collect IDs that actually exist, checking ! empty() instead would close that too (and 0 is never a valid attachment ID anyway). Could add it as a fourth provider case.
  1. Optional: the test only asserts on ids. Asserting on src as well would make sure the image URLs are unaffected.

#18 @abhishekfdd
2 weeks ago

## Test Report

### Patch tested
​https://github.com/WordPress/wordpress-develop/pull/14250, merged onto current trunk ([trunk revision]). Note the PR branch is behind trunk and could use a rebase.

### Environment

  • WordPress: [7.x-alpha, trunk rXXXXX]
  • PHP: [x.x]
  • Server: [ ]
  • Database: [ ]
  • OS: [ ]
  • Theme: [ ]
  • Plugins: None

### Reproduction
This uses dd32's case from comment:8, a Gallery block whose inner image blocks have no id attribute (external images):

`
<!-- wp:gallery {"linkTo":"file"} -->
<figure class="wp-block-gallery has-nested-images columns-default is-cropped">
<!-- wp:image {"width":"188px","height":"auto"} -->
<figure class="wp-block-image is-resized"><img src="​https://example.com/a.jpg" alt="" style="width:188px;height:auto" /></figure>
<!-- /wp:image -->
<!-- wp:image {"width":"251px","height":"auto"} -->
<figure class="wp-block-image is-resized"><img src="​https://example.com/b.jpg" alt="" style="width:251px;height:auto" /></figure>
<!-- /wp:image --></figure>
<!-- /wp:gallery -->
`

Called get_post_galleries( $post_id, false ) and get_post_galleries( $post_id, true ) via WP-CLI on these posts:

a) Gallery with no image IDs (above)
b) Mixed gallery, one attachment image and one external image
c) Gallery where all images have IDs (non-sequential order: 7, 5, 6)
d) Classic [gallery ids="..."] shortcode
e) Post with two galleries, one normal and one like (a)

### Results on trunk

  • The Undefined array key "id" warning fires for (a), (b) and (e), only with $html = false. The $html = true path never reads id.
  • Trunk also returns a malformed ids string: ',' for (a) and the second gallery in (e), and '5,' for (b). So this is wrong data returned to callers, not just a log warning.

### Results with the PR

  • ✅ Warning gone for (a), (b) and (e).
  • ✅ (b) now returns ids as '5', no empty entries or stray commas.
  • ✅ (c) and (d) output is identical to trunk in both modes, including the ids string, src array and ordering.

### Unit tests

  • ✅ The three data provider cases in tests/phpunit/tests/media/getPostGalleries.php pass with the PR.
  • ✅ Run against trunk's source, the two missing-ID cases fail with the ticket's warning and the all-IDs case passes, so the tests do cover the bug.
  • ✅ Full media group passes (975 tests, 7 skipped for PHP version and HEIC support, unrelated).
  • PHPCS: nothing on the changed lines.

### Notes
Left a few small review comments on the PR. The only one I'd want before commit is making the new data provider static to match the existing provider in that file, and that can be done on commit.

The reproduction above should cover needs-test-info, and the changes requested in comment:9 (combine the PRs, add a data provider) are done in #14250, so I think both needs-test-info and changes-requested can come off.

Note: See TracTickets for help on using tickets.