This repository was archived by the owner on Jan 19, 2026. It is now read-only.
Fix wp_upload_media JSON validation error with metadata#90
Open
Ref34t wants to merge 1 commit intoAutomattic:trunkfrom
Open
Fix wp_upload_media JSON validation error with metadata#90Ref34t wants to merge 1 commit intoAutomattic:trunkfrom
Ref34t wants to merge 1 commit intoAutomattic:trunkfrom
Conversation
The wp_upload_media tool was sending invalid JSON to WordPress when both file data and metadata (title, alt_text, caption, description) were provided. The issue was caused by the preCallback setting a raw binary body while the REST handler tried to add metadata parameters separately, creating malformed requests. This fix restructures the wp_upload_media_pre_callback to: - Create proper multipart/form-data format that includes both file and metadata - Use correct Content-Type header with boundary - Clear the args array since everything is now in the multipart body - Prevent parameter conflicts that caused JSON validation errors Changes: - Modified wp_upload_media_pre_callback() to generate RFC-compliant multipart bodies - Added comprehensive tests for multipart format validation - Added test for complete metadata handling (title, caption, description, alt_text) - Ensured backward compatibility with existing functionality Resolves Automattic#89
galatanovidiu
suggested changes
Aug 19, 2025
Contributor
galatanovidiu
left a comment
There was a problem hiding this comment.
Hi Ref34t,
Thank you for this PR, but unfortunately, this does not work for me.
I'm getting a 500 error (No Content-Disposition supplied) both on the unit test and on Claude code.
The Issue:
The WordPress REST API attachment controller expects a Content-Disposition header when uploading files via raw data, but the current multipart form implementation isn't providing it in the expected format.
Technical Details:
- The wp_upload_media_pre_callback creates multipart form data
- WordPress's upload_from_data() method looks for content_disposition in the request headers
- The multipart body structure isn't being parsed correctly by WordPress
The tests are failing with "Undefined array key 'content'" because the upload fails before returning the expected response structure.
Could you take a look at this? The multipart approach might need to be adjusted to work with WordPress's REST API expectations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The wp_upload_media tool was sending invalid JSON to WordPress when both file data and metadata (title, alt_text, caption, description) were provided. The issue was caused by the preCallback setting a raw binary body while the REST handler tried to add metadata parameters separately, creating malformed requests.
This fix restructures the wp_upload_media_pre_callback to:
Changes:
Resolves #89