• I’m extremely stumped by this error. Every time I try to save a draft in WordPress I get this message on Gutenberg. Tags/categories won’t save and I can’t publish. This post includes some logs and workarounds so I hope someone with better understanding of WordPress internals can figure out what is causing it.

    What Didn’t Work

    The error still persists after I:

    1. Disabled all plugins.
    2. Reverted to the default theme (twenty-twenty-four).
    3. Deleted all plugins and non-default themes.
    4. Updated wordpress to 6.4.3.
    5. Downloaded a fresh version of wordpress with wp core download.
    6. Ran wp rewrite flush.
    7. Changed my site’s home URL and domain on settings. (I couldn’t access it afterwards because I changed it to a random domain so I had to revert it by logging into phpmyadmin and changing it in the options…)
    8. Selected a category before saving a draft. (I’m really at my wit’s end.)

    I’m on NearlyFreeSpeech and I sort of followed their tutorial on how to install WordPress. Some relevant things I tried which also didn’t work:

    1. Removing .user.ini that autoadds Wordfence. (Wordfence is deactivated, by the way.)
    2. Running chgrp/chmod to grant write access to .htaccess and then saving the permalinks.
    3. Doing the same to wflogs (because I forgot to do it when I installed WP.)

    Workarounds Found

    From the internet I heard a possible “solution” would be installing the classic editor and using that instead of Gutenberg. I want to use Gutenberg, though, so that’s not really a solution, it’s just a workaround.

    Another workaround is to change the permalink structure to plain (i.e. ?p=123). That’s not really a solution either since I want my pretty links in the format /%category%/%post_id%/%postname%.

    Technical Details & Debug Logs

    With Chrome’s inspector open, I checked the network tab to see what was going wrong. Basically clicking the Save draft button sends a POST to:

    /wp-json/wp/v2/posts/123?_locale=user

    Where 123 is the post ID.

    Normally this API endpoint would return a 200 OK HTTP code and a response body including a JSON data for Gutenberg to interpret. In my bugged case, however, the wp-json API returns a 301 Moved Permanently with a location field to redirect to the homepage URL of the website. Since the homepage URL is a HTML page, whatever Javascript is handling the the save button click gets HTML code while they expected JSON, and that’s why it says “The response is not a valid JSON response” because it’s HTML code.

    So the question is why is it redirecting and how do we stop it? So, naturally, I downloaded my WP files and mysql database and tried to reproduce the error on localhost. I couldn’t reproduce it locally (I’m on fedora linux, btw). Maybe “localhost” was the problem, so I tried editing /etc/hosts and using Apache’s vhosts so I could access it from a local.example.com domain, but I still couldn’t reproduce the error.

    By the way, since 301 errors could potentially be cached, I had to do most of everything in this post with caching disabled on the browser, and because the site was also on a reverse proxy (Cloudflare), I had to put Cloudflare on development mode to bypass caching and that still had no effect.

    So I enabled DEBUG and DEBUG_LOG on my production site and checked the logs on /home/logs/error_log. It had a few warnings and errors (irrelevant details removed):

    PHP Error: PHP Warning: Undefined array key "REQUEST_METHOD" in …/wp-includes/pluggable.php on line 717, referer: …/wp-admin/post.php?post=123&action=edit
    
    PHP Error: PHP Warning: Undefined array key "path" in …/wp-includes/canonical.php on line 728, referer: …/wp-admin/post.php?post=123&action=edit
    
    PHP Error: PHP Notice: ob_end_flush(): Failed to send buffer of zlib output compression (0) in …/wp-includes/functions.php on line 5373, referer: …/wp-admin/post.php?post=123&action=edit

    This last one seems to be the real problem since it repeats a lot. Unfortunately, there’s just no way for me to debug this since I don’t know how to attach a breakpoint to PHP through SSH so I can go step to step into this to figure out what could possibly be causing it. So my only hope is that someone here knows how to fix this.

    Additional Notes

    One interesting thing is that when I click the save draft button, the content of the post is saved despite the error. What doesn’t get saved are categories, tags, and I think the post slug. Since there seems to be only one POST request, I think maybe the redirect is happening after it updates the content of the revision, but before it updates the tags? I’m not really sure to be honest. Perhaps if the POST succeeded Gutenberg would send another POST to update the tags?

    In any case, it seems this mightn’t be exactly a routing issue, as /wp-json/ is reachable correctly. I saw some posts saying that /wp-json/ errors could be fixed by changing the permalinks from plain to something else because otherwise the routing part of WP doesn’t work. Awkwardly, in my case it only works when I change the permalinks to plain. I’m not sure why since that does sound like a routing problem. As I mentioned, I tried flushing the rewrite rules without success. I also tried installing the rewrite-rules-inspector plugin but I couldn’t really find anything that seemed to show /wp-json/ rules wouldn’t work. Also I’m think Gutenberg makes a GET request to that API endpoint anyway at the start, so if it was routing it would be awkward if it only happened on POST and not on GET.

    Anyway, I have no idea how to solve this.

    Help.

    • This topic was modified 1 year, 1 month ago by othernoel. Reason: Submitted without tags, version
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Excellent effort in trying to resolve this for yourself! Too bad you could not, but great effort none the less.

    A 301 redirect from an API request is quite abnormal. Since you have tried with no plugins and default theme and no caching, I suspect some errant rule in .htaccess or similar directives in areas likely managed by your host such as httpd.conf or whatever the more modern equivalent is.

    Another possibility is an unusual modSecurity configuration. modSecurity should normally return a 403 Forbidden error, but I believe other behaviors can be configured. I’ve seen 404 Not Found returned, so maybe a 301 redirect is also possible.

    I’m sorry I cannot offer a definitive solution, but reaffirming that a 301 redirect is definitely unusual should be useful information. The challenge is finding the cause. It does seem to be outside of WP itself. If it’s not due to a .htaccess directive, it’s likely something odd about your hosting configuration.

Viewing 1 replies (of 1 total)
  • The topic ‘Updating failed. The response is not a valid JSON response.’ is closed to new replies.