Skip to content

fix: undefined array key 'context' warning in Gutenberg.php (PHP 8+)#943

Open
devinle wants to merge 1 commit intodevelopfrom
fix/undefined-context-key-warning
Open

fix: undefined array key 'context' warning in Gutenberg.php (PHP 8+)#943
devinle wants to merge 1 commit intodevelopfrom
fix/undefined-context-key-warning

Conversation

@devinle
Copy link

@devinle devinle commented Feb 2, 2026

Summary

  • Adds null coalescing check for context parameter in extend_post_content method
  • Prevents PHP 8+ "Undefined array key" warnings that corrupt JSON responses

Fixes #940

Problem

The extend_post_content method in Gutenberg.php accesses $params['context'] without checking if the key exists. In PHP 8.0+, this triggers an "Undefined array key" warning when the context query parameter is not explicitly provided in the REST API request.

This warning is output before the JSON response, corrupting the response body:

<br />
<b>Warning</b>: Undefined array key "context" in <b>.../Gutenberg.php</b> on line <b>90</b><br />
[{"id":123,...}]

Solution

Use the null coalescing operator to default to an empty string when context key doesn't exist:

// Before
if ( 'view' !== $params['context'] ) {

// After  
if ( 'view' !== ( $params['context'] ?? '' ) ) {

Test plan

  • Make REST API request without context parameter: GET /wp-json/wp/v2/pages?slug=test
  • Verify response is clean JSON without PHP warnings
  • Make REST API request with context parameter: GET /wp-json/wp/v2/pages?slug=test&context=view
  • Verify block_styles are still added to response when context=view

🤖 Generated with Claude Code


Note

Low Risk
Low risk: a small guard change that only affects the conditional gating of extend_post_content when the REST request omits context, preventing PHP 8+ warnings without changing behavior for context=view requests.

Overview
Prevents PHP 8+ "Undefined array key" warnings in Gutenberg::extend_post_content by safely handling missing REST request context (defaults to empty string) before deciding whether to extend the rendered content.

Adds a patch changeset entry for the @headstartwp/headstartwp release.

Written by Cursor Bugbot for commit 9338be8. This will update automatically on new commits. Configure here.

Add null coalescing operator to check for context parameter existence
before accessing it in extend_post_content method. This prevents PHP 8+
"Undefined array key" warnings that corrupt JSON responses when the
context query parameter is not explicitly provided in REST API requests.

Fixes #940

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Feb 2, 2026

🦋 Changeset detected

Latest commit: 9338be8

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@headstartwp/headstartwp Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel
Copy link

vercel bot commented Feb 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
headstartwp-app-router Ready Ready Preview, Comment Feb 2, 2026 8:11pm
headstarwp Ready Ready Preview, Comment Feb 2, 2026 8:11pm

Request Review

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.

Undefined array key "context" warning in Gutenberg.php line 90 (PHP 8+)

1 participant