Skip to content

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

Merged
nicholasio merged 2 commits intodevelopfrom
fix/undefined-context-key-warning
Feb 13, 2026
Merged

fix: undefined array key 'context' warning in Gutenberg.php (PHP 8+)#943
nicholasio merged 2 commits intodevelopfrom
fix/undefined-context-key-warning

Conversation

@devinle
Copy link
Contributor

@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
Single defensive check change plus a changeset; behavior only differs when context is missing, reducing warnings without affecting normal context=view requests.

Overview
Prevents PHP 8+ "Undefined array key" warnings (and potential JSON response corruption) when extend_post_content runs on REST requests that omit the context parameter by defaulting $params['context'] to an empty string.

Adds a patch changeset documenting the fix for @headstartwp/headstartwp.

Written by Cursor Bugbot for commit c33e94b. 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: c33e94b

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 13, 2026 1:16am
headstarwp Ready Ready Preview, Comment Feb 13, 2026 1:16am

Request Review

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

This PR is being reviewed by Cursor Bugbot

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

$params = $request->get_params();

if ( 'view' !== $params['context'] ) {
if ( 'view' !== ( $params['context'] ?? '' ) ) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default fallback value mismatches WordPress context default

Medium Severity

When the context key is absent from $params, the fallback defaults to '' instead of 'view'. WordPress's standard default for context in REST API endpoints is 'view', so a missing key semantically means context=view. With ?? '', the check 'view' !== '' evaluates to true and the method returns early, skipping block styles — the opposite of what happens when context is explicitly 'view'. Using ?? 'view' as the fallback would correctly match WordPress's intended default and ensure block styles are added for requests that omit the context parameter.

Fix in Cursor Fix in Web

@github-actions
Copy link
Contributor

📦 Next.js Bundle Analysis for @10up/wp-nextjs-app

This analysis was generated by the Next.js Bundle Analysis action. 🤖

🎉 Global Bundle Size Decreased

Page Size (compressed)
global 83.62 KB (🟢 -42.45 KB)
Details

The global bundle is the javascript bundle that loads alongside every page. It is in its own category because its impact is much higher - an increase to its size means that every page on your website loads slower, and a decrease means every page loads faster.

Any third party scripts you have added directly to your app using the <script> tag are not accounted for in this analysis

If you want further insight into what is behind the changes, give @next/bundle-analyzer a try!

@github-actions
Copy link
Contributor

📦 Next.js Bundle Analysis for @10up/headstartwp

This analysis was generated by the Next.js Bundle Analysis action. 🤖

This PR introduced no changes to the JavaScript bundle! 🙌

@nicholasio nicholasio merged commit 209fd26 into develop Feb 13, 2026
21 checks passed
@nicholasio nicholasio deleted the fix/undefined-context-key-warning branch February 13, 2026 01:38
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+)

2 participants

Comments