“Updating failed. The response is not a valid JSON response.” means the block editor sent a POST to /wp-json/wp/v2/posts/<id> and the reply would not parse as JSON. Something answered with HTML, a redirect, a PHP warning printed before the body, or nothing at all. Every fix starts with seeing that raw reply instead of the editor's one-line summary of it.

You need shell access to the site, permission to write a file into wp-content/mu-plugins/, and a browser with DevTools. Nothing else.

Three ways to see the response the editor is complaining about

Each one answers a different question, and starting with the wrong one is why people end up deactivating plugins at random.

DevTools shows what the browser received

Open the editor, press F12, switch to Network, filter by posts, then make an edit and watch for the red request. The Response tab gives you the status code and the first bytes of the body. It takes ten seconds and needs no server access. What it cannot tell you is why: a fatal error that killed PHP before it wrote anything, and a plugin that printed a warning before the JSON, both look like a short or empty body here.

curl separates WordPress from the web server

A terminal request carries no nonce and no cookies, so it cannot reproduce an authenticated save. It can still tell you whether /wp-json/ routes at all, whether the site redirects, and whether the server returns text/html where JSON is expected. That rules out permalinks, proxies and rewrite rules in one command.

A logger on the REST hooks catches the request you cannot reproduce

Hooking rest_post_dispatch and rest_pre_echo_response records the real authenticated request, its status, and any bytes printed before the JSON. This is the only one of the three that works when the failure is intermittent, happens only for editors, or comes from a plugin you cannot switch off on a live site.

Run curl first, because it is one command and eliminates the boring causes. When curl returns clean JSON and the editor still fails, install the logger.

Start with curl to rule out permalinks and redirects

# 1. Does the REST index answer at all? curl -i -s https://example.com/wp-json/ | head -n 20 # 2. The route the editor posts to curl -i -s https://example.com/wp-json/wp/v2/posts | head -n 20 # 3. Every hop, if a redirect is in play curl -sIL https://example.com/wp-json/wp/v2/posts | grep -Ei '^(HTTP|location|content-type)' # 4. The same route without rewrite rules curl -i -s 'https://example.com/?rest_route=/wp/v2/posts' | head -n 20 

Read the status line before anything else. HTTP/2 200 with content-type: application/json; charset=UTF-8 and a body starting with [{"id": means the route is healthy and the fault is further along. HTTP/2 301 or 302 with a location: header means the editor's POST is being redirected, and the target is an HTML page. HTTP/2 404 with text/html almost always means the server's rewrite rules are not passing /wp-json/ to WordPress.

The ?rest_route= form is the useful part. It bypasses rewrite rules entirely, so if that URL returns JSON and /wp-json/ returns HTML, the problem is the rewrite setup and nothing else.

Check the structure with wp option get permalink_structure. If it prints a pattern such as /%postname%/, WordPress builds every REST URL as /wp-json/… and relies on the web server to hand those requests to index.php. When that hand-off is missing, the editor's save gets the server's own HTML 404 page. On Apache, check that .htaccess contains the WordPress block and that AllowOverride is not set to None; saving Settings → Permalinks once rewrites the file and flushes the rules. On nginx there is no .htaccess, so the try_files $uri $uri/ /index.php?$args; line has to cover /wp-json/ like any other permalink.

If it prints nothing, the site is on Plain permalinks. The editor then sends its requests to ?rest_route= instead of /wp-json/, so rewrites are not your problem. Switching to Plain is a popular workaround because it sidesteps broken rewrite rules, but it hides the fault rather than fixing it, and it changes every public URL on the site.

Settings → Permalinks with Post name selected: WordPress now sends REST requests to /wp-json/, which works only if the server's rewrite rules pass them to index.php (test site: WordPress 7.1.3, PHP 8.2)

Log the real request with a small mu-plugin

An mu-plugin loads before regular plugins, cannot be deactivated from the admin, and is not touched by updates. Save this as wp-content/mu-plugins/rest-json-logger.php, creating the directory if it does not exist.

<?php /** * Plugin Name: REST JSON Response Logger * Description: Records the REST response the block editor receives, plus anything printed before it. */ const WPTUTSHUB_REST_LOG = WP_CONTENT_DIR . '/rest-json.log'; add_filter( 'rest_post_dispatch', function ( $response, $server, $request ) { $GLOBALS['wptutshub_rest_last'] = array( 'response' => $response, 'request' => $request, ); return $response; }, 10, 3 ); add_filter( 'rest_pre_echo_response', function ( $data, $server ) { $last = $GLOBALS['wptutshub_rest_last'] ?? null; if ( ! $last ) { return $data; } $request = $last['request']; $response = $last['response']; $status = $response instanceof WP_REST_Response ? $response->get_status() : 500; $method = $request->get_method(); // Skip successful reads, or every editor page load fills the file. if ( 'GET' === $method && $status < 400 ) { return $data; } $stray = ''; if ( ob_get_level() > 0 ) { $stray = (string) ob_get_contents(); } $sent_at = ''; if ( headers_sent( $file, $line ) ) { $sent_at = $file . ':' . $line; } $entry = array( 'time' => gmdate( 'c' ), 'method' => $method, 'route' => $request->get_route(), 'status' => $status, 'user_id' => get_current_user_id(), 'stray_bytes' => strlen( $stray ), 'stray_head' => substr( $stray, 0, 300 ), 'headers_sent_at' => $sent_at, 'body_head' => substr( (string) wp_json_encode( $data ), 0, 300 ), ); error_log( wp_json_encode( $entry ) . "\n", 3, WPTUTSHUB_REST_LOG ); return $data; }, 10, 2 ); 

Two hooks do the work. rest_post_dispatch fires inside dispatch() with the finished WP_REST_Response, so it has the status code and the route, and it also fires for internal calls made through rest_do_request(). rest_pre_echo_response fires in serve_request() a few lines before WordPress echoes the JSON, which is the last moment you can look around and see whether something already printed. That second hook never runs for rest_do_request(), only for real HTTP requests. It is the right tool for editor failures and the wrong one for unit tests.

The stray-output check has two branches because output escapes in two ways. If a plugin started an output buffer, ob_get_contents() holds everything printed so far. If nothing is buffering, the bytes went straight to the client and headers_sent() names the file and line that forced the headers out. Either one explains the editor's message.

Successful GET requests are skipped on purpose. The editor makes a lot of them, and a log that grows by a megabyte per editing session is a log nobody reads.

Reading the log: stray bytes, status codes and the route that failed

Each line is one JSON object. The fields that matter:

  • stray_bytes above zero is the answer. stray_head shows the text, and headers_sent_at names the file and line when nothing was buffering. A single blank line after a closing ?> is enough to break the save.
  • status 401 with user_id 0 means WordPress did not recognise the user, usually because a proxy stripped the Cookie header. An expired nonce shows up as a 403 with the code rest_cookie_invalid_nonce instead. Both replies are valid JSON, so they explain a different editor message, not this one; the last section covers them.
  • status 403 is a permission or firewall answer. If body_head contains HTML rather than a JSON error object, a security plugin or the host's WAF replied before WordPress did.
  • status 404 on a route you know exists points back at rewrite rules, or at a siteurl that does not match the host in the address bar.
  • No line at all while DevTools shows a 500 means PHP died. A fatal error stops the request before either hook runs, so the PHP error log is the next file to open. A 500 that does get logged came from WordPress itself, and body_head holds its error code and message.

If the file stays empty while the editor still fails and DevTools shows anything other than a 500, the request is not reaching WordPress at all. That is what the next section handles.

Separate a WordPress fault from a server fault with WP-CLI

Save this as /tmp/rest-check.php and run wp eval-file /tmp/rest-check.php from the site root.

<?php // Dispatch inside PHP. No web server, no proxy, no rewrite rules. $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); ob_start(); $response = rest_do_request( $request ); $noise = ob_get_clean(); printf( "internal status: %d\n", $response->get_status() ); printf( "internal items: %d\n", count( (array) $response->get_data() ) ); printf( "output before the body: %d bytes\n", strlen( $noise ) ); 

On a healthy site it prints this:

internal status: 200 internal items: 10 output before the body: 0 bytes 

rest_do_request() runs the route inside PHP with no web server, no proxy and no .htaccess. A 200 with a sensible item count means WordPress and the route are healthy, and the fault sits between the browser and PHP. Anything above 0 bytes on the last line was printed by a plugin or theme while the route ran, which is the same stray output the logger looks for.

If the internal status is 200 and curl returns HTML, look at the server: a proxy buffering the response, a WAF rule on POST /wp-json/, or an http to https redirect that the editor's fetch follows and then parses as JSON. If the internal status is broken too, the problem is inside WordPress and the log will say which hook did it.

Fixes for what the log usually shows

Security rules and proxies that answer JSON with HTML

A 403 whose body starts with <!DOCTYPE html> did not come from WordPress. Turn off the security plugin's “disable REST API” option first, because several of them block /wp-json/ for logged-out users and forget that the block editor is authenticated but still passes through the same rules. If the body is a generic host error page, ask the host which rule fired and to switch that one rule off for /wp-json/ only. Do not ask for an exemption based on the X-WP-Nonce header: anyone can send that header, so the exemption would turn the firewall off for every attacker who adds it. On shared hosting the firewall is usually ModSecurity with the OWASP Core Rule Set. Its rule 949110 does the blocking once a request's anomaly score is too high, so ask for the earlier rule that raised the score and exclude that one.

The same rule applies to routes you write yourself: a route that returns HTML on failure produces this exact editor message, so return WP_Error and let WordPress encode it. The patterns in securing custom REST API endpoints with nonces and capability checks cover that side of it, and auditing REST routes for missing permission callbacks catches the ones that answer with a fatal instead.

Stray output from a plugin, a BOM or a closing PHP tag

When stray_bytes is above zero, the text in stray_head is usually a PHP notice, a debug line someone left behind, or a byte order mark. A BOM is invisible in most editors and arrives when a file is saved by Notepad or by a tool that defaults to UTF-8 with signature. Closing ?> tags are optional in PHP files and any whitespace after one is sent to the browser before WordPress starts, which is why the coding standards tell you to leave them out.

# Byte order marks at the top of any PHP file (needs GNU grep, as on most Linux servers) grep -rlP --include='*.php' '^\xEF\xBB\xBF' wp-content/plugins wp-content/themes wp-content/mu-plugins # Closing tags in a theme's functions file (remove them, they are optional) grep -rn '?>' wp-content/themes/your-theme/functions.php # The PHP error log, if WP_DEBUG_LOG is on tail -n 40 wp-content/debug.log 

Fix the file the log names, then reload the editor and save again. The log line for the retry should show stray_bytes at 0 and a 200 status, which is the point where the editor stops complaining.

When the editor shows a different message: nonces and cookies

Not every failed save is this error. When the reply is valid JSON, the editor prints WordPress's own message instead, and two of them come up often. “Cookie check failed” is a 403 with the code rest_cookie_invalid_nonce: the nonce the editor sent has expired. The editor renews its nonce in the background while the tab is open, so this mostly happens after a laptop wakes from sleep or when a plugin has switched off the Heartbeat API, and reloading the editor fixes it. If every editor fails immediately after logging in, check whether a plugin hooks rest_authentication_errors and returns a WP_Error for requests it does not recognise; that filter runs before the route's own permission callback and will reject the editor's nonce if the plugin only checks for an application password.

A 401 with user_id 0 in the log means the login cookie never reached WordPress, and the editor says you are not allowed to edit the post. A reverse proxy that drops the Cookie header, a COOKIE_DOMAIN constant that does not match the current host, or a half-finished domain move will all do it. If the site recently changed address, replacing the old URLs after a domain change is the step that usually fixes the REST calls too, because siteurl and home decide which host the editor talks to.

Once the log has done its job, delete the mu-plugin. If you would rather keep the diagnostic around, make it self-limiting: rotate the file when it passes a few hundred kilobytes, and have rest_pre_echo_response return a JSON error response when it detects stray bytes, so the editor shows a real message instead of the generic one. That turns a debugging aid into a guard that survives the next badly written plugin.

Originally published on WP Tuts Hub.