Skip to content

Return errors from wp_insert_post / wp_update_post failures - #320

Merged
pfefferle merged 1 commit into
trunkfrom
fix/issue-319-error-response-status
May 2, 2026
Merged

Return errors from wp_insert_post / wp_update_post failures#320
pfefferle merged 1 commit into
trunkfrom
fix/issue-319-error-response-status

Conversation

@pfefferle

Copy link
Copy Markdown
Member

Summary

Fixes #319.

When wp_insert_post or wp_update_post returned a WP_Error, check_error() wrapped it as a Micropub Error and stored it in $args['ID'], but handle_create() / handle_update() returned the $args array as if the operation had succeeded. The endpoint then responded with HTTP 201 and a body that included the error nested inside the ID field, plus post_url: false and Location: false.

  • Propagate the Micropub error from handle_create() and handle_update() when insertion fails, so create_item() returns a proper error response.
  • Change the default status in micropub_wp_error() from 200 to 400. A WP_Error always indicates failure, so a 200 default never made sense; this also surfaces the correct status for wp_insert_post's built-in errors (like empty_content) which carry no status data.

Test plan

  • New regression test test_create_returns_error_when_wp_insert_post_fails reproduces the exact scenario from Errors return HTTP 201 status #319 (uses the wp_insert_post_empty_content filter to force the error). Fails on trunk (returns 201), passes with the fix (returns 400 with error: empty_content).
  • Full PHPUnit suite passes (86 tests, 337 assertions).
  • PHPCS clean on changed files.

Previously, when wp_insert_post or wp_update_post returned a WP_Error,
the error was wrapped and stored in $args['ID'] but the array was
returned as if creation succeeded. The endpoint then responded with
HTTP 201 and a body containing the error nested inside the ID field.

Propagate the Micropub error from handle_create() and handle_update()
when insertion fails, and default micropub_wp_error()'s status to 400
instead of 200 since a WP_Error always indicates failure.

Fixes #319
@dshanske

dshanske commented May 2, 2026

Copy link
Copy Markdown
Member

This makes sense to me

@dshanske
dshanske self-requested a review May 2, 2026 17:51
@pfefferle
pfefferle merged commit 19101f7 into trunk May 2, 2026
4 checks passed
@pfefferle
pfefferle deleted the fix/issue-319-error-response-status branch May 2, 2026 18:11
@pfefferle pfefferle mentioned this pull request Jul 8, 2026
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Errors return HTTP 201 status

2 participants