[wp-trac] [WordPress Trac] #63394: preg_match() warning in wp_image_add_srcset_and_sizes() when image_meta['file'] is missing

WordPress Trac noreply at wordpress.org
Mon May 5 10:42:48 UTC 2025


#63394: preg_match() warning in wp_image_add_srcset_and_sizes() when
image_meta['file'] is missing
-------------------------------+-----------------------------
 Reporter:  enravo             |      Owner:  (none)
     Type:  defect (bug)       |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Media              |    Version:  6.8
 Severity:  normal             |   Keywords:  has-patch
  Focuses:  php-compatibility  |
-------------------------------+-----------------------------
 In the `wp_image_add_srcset_and_sizes()` function (`wp-
 includes/media.php`), the `$image_meta['file']` key is used without
 checking if it exists.

 When image metadata is incomplete or malformed (e.g. missing the `file`
 key), this causes the following issues on PHP 8+:

 ---

 ###  Sample Error (PHP 8+):
 Warning: Undefined array key "file" in wp-includes/media.php on line 1768

 {{{
 Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type
 string is deprecated
 }}}

 ###  Problem Line (media.php:1768):
 {{{#!php

 if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) )

 }}}
 Wrap the logic with a check for 'file' to ensure safe execution:
 {{{#!php

 if ( ! empty( $image_meta['file'] )
      && preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash )
      && ! str_contains( wp_basename( $image_src ), $img_edit_hash[0] ) ) {
     return $image;
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/63394>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list