Skip to content

Conversation

@Hsu-Pei-Chun
Copy link

@Hsu-Pei-Chun Hsu-Pei-Chun commented Dec 24, 2025

Problem

The add_rest_api_headers method assumes $response is always a WP_REST_Response object, but the rest_pre_serve_request filter can pass false in some cases.

This causes a fatal error when WP_DEBUG is enabled or when other plugins trigger warnings that cause "headers already sent":

Fatal error: Uncaught Error: Call to a member function header() on false in wp-content/themes/761-main/functions.php on line 82

Solution

Add an instanceof check before calling the header() method:

public function add_rest_api_headers($response) {
    if ($response instanceof WP_REST_Response) {
        $response->header('X-Robots-Tag', 'noindex, nofollow');
    }
    return $response;
}

This ensures the method only attempts to add headers when $response is a valid WP_REST_Response object.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed REST API response header handling to prevent improper header application.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 24, 2025

📝 Walkthrough

Walkthrough

The add_rest_api_headers function in functions.php now includes a conditional check before setting the X-Robots-Tag header. The header is only applied when the response is an instance of WP_REST_Response, adding type safety to the header modification.

Changes

Cohort / File(s) Summary
REST API Header Guard
functions.php
Added type check in add_rest_api_headers() to conditionally set X-Robots-Tag header only for WP_REST_Response instances instead of unconditionally

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A guard clause stands so tall and true,
Checking types before headers shine through,
Type safety blooms in REST API's domain,
No more unconditional header rain! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding a type check in add_rest_api_headers to prevent a fatal error when the response is not a WP_REST_Response object.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant