gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

Post editor: saving after switching to and editing template on new post returns 400

Open ramonjd opened this issue 2 years ago • 1 comments

Description

On new posts with no content, switching to (or creating), editing then saving a template triggers the "empty_content" server error.

POST response from /index.php?rest_route=/wp/v2/posts/246&_locale=user

{
    "code": "empty_content",
    "message": "Content, title, and excerpt are empty.",
    "data": {
        "status": 400
    }
}

The post is 'technically' dirty as the template slug has been updated on switch via editPost, hence the reason it appears in the saved entities list.

Screenshot 2023-12-07 at 11 36 31 am

Alternatives in no particular order of usefulness

  • Allow saving in this instance without content/title/excerpt changes
  • Save only the template and "unmark" the post entity as dirty, maybe turning off template editing mode and returning to the post afterwards
  • Warn the user that the post is empty 🤷🏻

Step-by-step reproduction instructions

  1. Open the post editor
  2. Switch templates using the drop down in the Template panel (right hand side). Creating a new template also triggers the error.
  3. Edit the template
  4. Make a change to the template
  5. Save the post (site and post entities)
  6. Observer the UI error message and network error (400)

What I expected

The template and post (with updated template slug) to save without error.

Screenshots, screen recording, code snippet

Creating a template:

https://github.com/WordPress/gutenberg/assets/6458278/51b933d0-c6d6-437c-bf56-6ec4bb2411c4

Swapping to a template:

https://github.com/WordPress/gutenberg/assets/6458278/decb2df7-54f6-47a4-88e8-45819347c2b6

Environment info

  • WordPress 6.5 alpha
  • Gutenberg trunk @ 17.2

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

ramonjd avatar Dec 07 '23 00:12 ramonjd

I investigated the issue and found that the core problem stems from the template assignment being treated as a non-transient edit on the current post. This causes the post to appear as dirty, even when it's otherwise empty (no title, content, or excerpt). As a result, trying to save it fails.

One possible solution would be to skip counting the template change as a non-transient edit when the post is otherwise clean, kind of mimicking the behavior when a template is just swapped, rather than edited or newly created, essentially treating it as a no-op in that specific scenario.

I’ll explore a patch along these lines. Let me know if there are any edge cases I might be overlooking.

BugReportOnWeb avatar Jul 11 '25 18:07 BugReportOnWeb