Opened 4 weeks ago
Last modified 4 weeks ago
#63577 new defect (bug)
PHP Warning from `get_post_galleries()` when gallery items lack an ID.
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | 6.9 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Gallery | Keywords: | has-patch has-unit-tests needs-test-info |
Focuses: | Cc: |
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 (8)
This ticket was mentioned in PR #8992 on WordPress/wordpress-develop by @dd32.
4 weeks ago
#1
- Keywords has-patch added
#4
@
4 weeks 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
- Go to Posts > Add New.
- Open the Code Editor in the block editor.
- Paste one of the following shortcode using Shortcode block (any of these will trigger the issue):
[gallery]
or
[gallery ids]
or
[gallery ids=""]
- Publish the post.
- 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>'; });
- Visit the post on the frontend.
Actual Results
✅ PHP Warning appears in the debug log:
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
This ticket was mentioned in PR #9005 on WordPress/wordpress-develop by @rollybueno.
4 weeks 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
#7
@
4 weeks 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
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
And no gallery at all as expected:
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
#8
@
4 weeks 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 -->
Trac ticket: https://core.trac.wordpress.org/ticket/63577