Skip to content

REST API: Prevent fatal error when a null template reaches prepare_item_for_response() - #13375

Open
ramonjd wants to merge 1 commit into
WordPress:trunkfrom
ramonjd:fix/prevent-null-template-fatal
Open

REST API: Prevent fatal error when a null template reaches prepare_item_for_response()#13375
ramonjd wants to merge 1 commit into
WordPress:trunkfrom
ramonjd:fix/prevent-null-template-fatal

Conversation

@ramonjd

@ramonjd ramonjd commented Sep 3, 2026

Copy link
Copy Markdown
Member

Trac ticket: https://core.trac.wordpress.org/ticket/66032

Backports the guard from WordPress/gutenberg#82374.

What

Prevents an uncaught fatal error (Attempt to assign property "content" on null) in WP_REST_Templates_Controller::prepare_item_for_response() when it receives a null template.

Why

WP_REST_Templates_Controller::update_item() passes its get_block_template() refetches to prepare_item_for_response() without checking them, both after writing an update and on its revert-to-theme path, which force-deletes the template's post before checking that a theme or plugin version of the template exists. When a refetch returns null, the method fatals reading $item->content.

What changed

  • prepare_item_for_response() returns a rest_template_not_found WP_Error (404) when the template is null, instead of crashing.
  • Adds a unit test that calls prepare_item_for_response( null, ... ) directly, the exact condition that fatals on trunk. Without the guard the call fatals and the run aborts, so the test can only pass through the guard.

Manual testing

The revert path is the easiest way to reach the fatal:

  1. Create a template that exists only in the database: POST /wp/v2/templates with { "slug": "db-only", "title": "T", "content": "" }.
  2. Send PUT /wp/v2/templates/<theme>//db-only with { "source": "theme" }.
  3. On trunk this fatals with the error above; with this branch it returns 404 rest_template_not_found.

…em_for_response().

WP_REST_Templates_Controller::update_item() passes its
get_block_template() refetches to prepare_item_for_response() without
checking them, both after writing an update and on its revert-to-theme
path, which force-deletes the template's post first. When a refetch
returns null, reading $item->content is a fatal error.

Return a rest_template_not_found error (404) instead when the template
is null.

Backports the guard from Gutenberg PR 82374.

See #66032.
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props ramonopoly.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@ramonjd ramonjd self-assigned this Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants