Opened 5 months ago
Last modified 5 months ago
#63028 new defect (bug)
PHP Warning caused by media.php and formatting.php
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 6.7.2 |
Component: | Media | Keywords: | reporter-feedback close |
Focuses: | Cc: |
Description
PHP Warning in media.php and formatting.php
Description
A PHP warning occurs due to an "Array to string conversion" error during media processing. The error logs report:
#6 {main} thrown in /wp-includes/media.php on line 4531 [25-Feb-2025 17:59:27 UTC] PHP Warning: Array to string conversion in /wp-includes/formatting.php on line 1096
Steps to Reproduce
- Process media uploads where the attachment's post parent is set as an array.
Proposed Fix
Ensure the post parent is treated as a scalar value. For example, modify the code in media.php as follows:
if ( ! empty( $attachment->post_parent ) ) { $post_parent_id = is_array( $attachment->post_parent ) ? reset( $attachment->post_parent ) : $attachment->post_parent; $post_parent = get_post( $post_parent_id ); if ( $post_parent ) { $response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' ); $response['uploadedToLink'] = get_edit_post_link( $post_parent_id, 'raw' ); } }
Impact
This fix will prevent PHP warnings during media processing and ensure proper handling of attachment data.
Change History (5)
#1
follow-up:
↓ 3
@
5 months ago
- Component changed from General to Media
- Keywords reporter-feedback added
#2
@
5 months ago
[25-Feb-2025 17:59:27 UTC] PHP Warning: Array to string conversion in /wp-includes/formatting.php on line 1096
This is from wp_check_invalid_utf8()
with invalid input, probably a valid PHP Warning, as something is calling it with invalid input. Information that can point to the cause of that warning would be needed to determine what the actual source is.
#3
in reply to:
↑ 1
;
follow-up:
↓ 4
@
5 months ago
To simulate the condition where an attachment’s post parent is an array, even though in a normal WP_Post
object it should be a single, scalar value:
// Assume $attachment_id is a valid attachment ID. $attachment = get_post( $attachment_id ); // Normally, $attachment->post_parent is a scalar (e.g. an integer). // For testing, we deliberately convert it into an array to replicate the error. $attachment->post_parent = array( $attachment->post_parent ); // Now, when processing the attachment, this will trigger the "Array to string conversion" warning. if ( $attachment->post_parent ) { // This line will produce a warning because $attachment->post_parent is an array. $post_parent = get_post( $attachment->post_parent ); }
A WP_Post
object's post_parent
must be a single scalar (typically an integer). If it's an array, it triggers PHP warnings and risks unpredictable behaviour, compromising site stability.
Replying to dd32:
Process media uploads where the attachment's post parent is set as an array.
Can you provide an example to duplicate that?
$attachment
here should be aWP_Post
object, which can only have a singular scalar ID.
#4
in reply to:
↑ 3
;
follow-up:
↓ 5
@
5 months ago
Replying to isrgrajan:
To simulate the condition where an attachment’s post parent is an array
Doing so is completely incorrect, and not something that WordPress should be supporting, IMHO.
There's only so much WordPress can do to protect developers from themselves.
Ideally the WP_Post::post_parent
field would have int validation on the values that can be set, but unfortunately PHP doesn't really support that in a way that WordPress can use for back-compat.
If it's an array, it triggers PHP warnings and risks unpredictable behaviour, compromising site stability.
Exactly. A developer should expect unpredictable result, including PHP Warnings and Fatal errors, by setting data to values outside of the expected types.
#5
in reply to:
↑ 4
@
5 months ago
- Keywords close added
Replying to dd32:
Thank you for the clarification. I completely agree—forcing the post_parent
into an array is not something WordPress should support. The simulation was only intended to illustrate how unexpected input can lead to PHP warnings and unstable behaviour, emphasizing that developers need to ensure they're passing data in the expected format. In a proper implementation, WP_Post::post_parent
should always be a scalar, and any deviation is a developer error rather than a WordPress bug. Your insights are much appreciated.
Can you provide an example to duplicate that?
$attachment
here should be aWP_Post
object, which can only have a singular scalar ID.