The Linux Page

Publishing failed. Error message: The response is not a valid JSON response

Beep Brake -- you get distracted, we don't!

I installed a new Wordpress website and I was getting this error when trying to save pages and also when trying to go to a page once I switched the permalink to "Post name".

This is not a very clear error. Plus all the admin functionality was working like a charm so it took me close to forever to fix this one. (like a good 6 hours!)

The fact is I've got three a domain name with three different TLDs:

When I create the configuration file for Apache, I used another one which I just created and just renamed the domain.

In my case, I missed one .com which had to be converted to .app and the permalinks started failing.

It took me forever to think that maybe the special .htaccess code would not work properly. Here is my Apache2 Directory block that used the wrong TLD:

<Directory "/var/www/">                       
    Options -Indexes                                                
    DirectoryIndex index.php                                        
    AllowOverride All                                               
    # For clean permalinks -- see .htaccess provided by Wordpress   
    RewriteBase /                                                   
    RewriteRule ^index\.php$ - [L]                                  
    RewriteCond %{REQUEST_FILENAME} !-f                             
    RewriteCond %{REQUEST_FILENAME} !-d                             
    RewriteRule . /index.php [L]                                    

As we can see, the path in the <Directory> tag says "". It was supposed to be ".app" at the end. (the .com and .org redirects to the .app).

Now, the .htaccess will also be loaded since I have the AllowOverride option. So my Rewrite... code here is not relevant. But that's a verbatim copy from the .htaccess except for the fact that I don't test whether the module is turned on and I know that the module is turned on so I don't do that either.

How did I test whether it was working on not?

At some point, I finally had the thought that maybe the hits weren't even reaching the index.php file at all. So I decided to edit the file and test to make sure.

I simply added a die() command at the top to test, like so:

 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 * @package WordPress

die("Is index.php even reached?")

 * Tells WordPress to load the WordPress theme and output it.
 * @var bool
define( 'WP_USE_THEMES', true );

/** Loads the WordPress Environment and Template */
require __DIR__ . '/wp-blog-header.php'

Note that line will completely break your wordpress system but it is useful to verify that you fixed the permalink. Once I had the .app instead of .com and restarted Apache2 like so:

sudo systemctl restart apache2

Then I would see the "Is index.php even reached?" message in my browser. After that, I deleted that line and everything works like a charm!

I hope this helps you resolve your own problems. The error mentioned in the title:

Publishing failed. Error message: The response is not a valid JSON response

would appear in my browser Network tab as a 404 and I just couldn't think that maybe this happens because the index.php doesn't get hit properly.

# JSON hit without the Permalink feature

# JSON hit with the Permalink feature turned ON

Now you know, if you get a 404 for a page that exists and for /wp-json, then you have to fix the index.php setup in your Apache2 configuration files.