Make WordPress Core

Opened 8 months ago

Last modified 3 months ago

#63577 new defect (bug)

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

Reported by: dd32's profile dd32 Owned by:
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Gallery Keywords: has-patch has-unit-tests needs-test-info changes-requested
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 (11)

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


8 months ago
#1

  • Keywords has-patch added

@dd32 commented on PR #8992:


8 months ago
#2

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

#3 @swissspidy
8 months ago

  • Keywords needs-unit-tests added
  • Milestone changed from Awaiting Review to 6.9

#4 @rollybueno
8 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.


8 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
8 months ago

  • Keywords needs-testing added

#7 @SirLouen
8 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 gallery at all as expected:
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

Last edited 8 months ago by SirLouen (previous) (diff)

#8 @dd32
8 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
3 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
3 months ago

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

#11 @wildworks
3 months ago

  • Milestone changed from 6.9 to 7.0
Note: See TracTickets for help on using tickets.