[wp-trac] [WordPress Trac] #63577: PHP Warning from `get_post_galleries()` when gallery items lack an ID.

WordPress Trac noreply at wordpress.org
Wed Jun 18 04:49:21 UTC 2025


#63577: PHP Warning from `get_post_galleries()` when gallery items lack an ID.
-------------------------------------------------+-------------------------
 Reporter:  dd32                                 |       Owner:  (none)
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  6.9
Component:  Gallery                              |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch needs-unit-tests has-      |     Focuses:
  test-info                                      |
-------------------------------------------------+-------------------------
Changes (by rollybueno):

 * keywords:  has-patch needs-unit-tests => has-patch needs-unit-tests has-
     test-info


Comment:

 == 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=""]
 }}}
 4. Publish the post.
 5. 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>';
 });
 }}}
 6. Visit the post on the frontend.

 === Actual Results
 ✅ PHP Warning appears in the debug log:
 [[Image(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:
 [[Image(https://i.imgur.com/XIZDX00.png)]]

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63577#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list