WordPress’ Post name Permalinks, Block Editor, and JSON Error

Following the right path makes finding clues easier.

Stated in many posts on this site is the lack of a traffic generating goal. When I first set up the site permalinks defaulted to “Plain” or I set them to “Plain.” I don’t remember. Every URL for a page, post, comment, or view ended with something like /?p=59 or /?p=128. It kept the URLs short but provided no other real benefit.

Recently I began to think about changing the permalinks to title style. I now appreciate having some description of the page in the URL and wanted to enable it for this site. Plus it is supposed to help with search engine ranking if the URL is descriptive. Simple enough, change the Permalink Structure from Plain to Post name.

Not so simple.

The change saves without error and the site functions as expected when browsed afterward. The URLs all include the title in the link rather than ending /?p=xx. All seems well. Then edit an existing draft or published post or create one. When saved as draft or attempting to publish, an invalid JSON message is displayed. The edits, or new post, will not be saved and cannot be published!

Start tracking down “not a valid JSON response” with “WordPress” and got plenty of hits. As is usual when there’s lots of hits, there is agreement across articles about many of the settings to review for a solution. And none of the common solutions applied! The conditions were already as specified or making the change didn’t make saving edits in the block editor possible. Same for the solutions that weren’t common to all articles. Either conditions already as specified or the change didn’t resolve the problem.

I turned off the block editor and used the classic editor. Posts could be edited and saved and new posts could be created and saved as draft or published. The site was working, and with Post name permalinks, but nothing could be edited or created with the block editor, the classic editor needed to be used.

Trying to get a handle on the problem I reviewed the server logs, the web server logs, enabled and reviewed the WordPress logs, monitored console output in my browser. I also tried editing from different operating systems and with different browsers. The problem was consistent regardless the OS or browser used while editing. All I was able to find was that files in a wp-json folder weren’t being found.

With this information I searched the server from / for a path that includes wp-json. There is no path with that string. Now a new search focus for troubleshooting. Rather than WordPress and the JSON error, WordPress and missing wp-json module. Eventually I found some articles that recommended AllowOverride all be enabled for the web server’s directory statement.

I tested and it worked! Post name Permalinks could be enabled and the block editor worked as expected. But I couldn’t reconcile enabling that directive for all sites on the server. Fortunately the <Directory> statement can be in a <VirtualHost> statement. This site’s <VirtualHost> statement now contains a <Directory> statement with AllowOverride all. Restart the server and *boom* the site works and editing in the block editor works.

It is surprising to me that not one of the WordPress related troubleshooting articles I found, that had in agreement at least a handful of steps among them, ever mentioned the Apache <Directory> statement.

The troubleshooting tree that finally worked, tracking down reasons for missing wp-json path, wasn’t hinted at in the initial error message, searches with “WordPress” and “the response is not a valid JSON response” kept turning up the same potential causes, none of which included discovering the missing wp-json, how to troubleshoot it, or that a web server configuration requirement, not a WordPress setting, resolved the issue.

When troubleshooting keep digging, and try different searches if what first turns up doesn’t help. Need to keep digging until the corrective action is found. Even if the cause or messages from it can’t be found. Apache log level was set to info and there was nothing with the term “wp-json” found in any log.

Confirmed, Movies Updates Work

House of cards, but with the stack setup it is easier.

Like many things that appear on a computer screen there is a long chain of events that need to happen successfully for what is on screen to be what is desired there. The various “Movies” tables on this website are one example.

I got some DVDs for Christmas. A very nice Seven Samurai DVD with extras. My movies database had it marked as movie I want. It appeared on this website in the Movies I Want tables that are on two pages of this website.

Change the setting in the database so it’s a movie I have and the title should now only be found in the two Movies I Have tables on the website and no longer found in the two Movies I Want tables on this website. One change to the source to trigger four changes on the website.

In the case of the Movies tables, for changes to the movies database to appear in the tables, the updated data must be exported to two files, one listing “Movies I Have” and the other “Movies I Want”. Those exported files update the source lists the Movies tables refer to. And finally a sync tool from the wpTables publisher must run against the source lists to update the Movies tables on the website.

Making changes to the movies database is infrequent, a few times a year at most. Remembering the process each time is a challenge but now the data extract step and the link refresh steps are automated which makes most of the process happen without need to remember anything (or look at the code if I wish to remember).

The link update code as a cron…

# m h  dom mon dow   command
*/15 * * * * wget -q -O – "https://wp.boba.org/wp-admin/admin-ajax.php?action=wdtable_update_cache&wdtable_cache_verify=<hex-number>"

Export updates for source lists …

<?php
 $server = "<host>"; 
 $username = "<user-name>";
 $password = "<pwd>"; 
 $database_name = "<movies>"; 
 $link_myMovies = mysqli_connect($server, $username, $password, $database_name);
 $Views = array("movies_i_want","movies_i_have");
 $out_path = "/var/tmp/";
 
 foreach ($Views as $view)
 {
        $query_result = null;
        $columns_total = null;
        $str_query = "SELECT * FROM $view";
        $query_result = mysqli_query($link_myMovies, $str_query);
        $columns_total = mysqli_num_fields($query_result);
 
        $col_names = array();
 
        for ($i = 0; $i < $columns_total; $i++) 
         {
                $Heading = mysqli_fetch_field_direct($query_result, $i);
                array_push($col_names,$Heading->name);
         }
 
         $fileOut = fopen("$out_path$view.csv", 'w') or die("Unable open ./$out_path$view.csv");
         fputcsv($fileOut, $col_names);

         while ($row = mysqli_fetch_array($query_result, MYSQLI_NUM)) 
         {
                fputcsv($fileOut, array_values($row)); 
         }
         
         fclose($fileOut) or die("Unable to close ./$view.csv");
}
?>

Perils of a part time web server admin

Not being “in it” all the time can make simple things hard.

Recently one of the domain names I’ve held for a while expired. Or actually, I let it expire. It was hosted on this same web server along with several other websites and had a secure connection using a Lets Encrypt SSL certificate. All good.

The domain name expired, I disabled the website, and all the other websites on the server continued to be available. Until they weren’t! When I first noticed I just tried restarting the web server. No joy, that didn’t get the other sites back up.

And here’s the perils of part time admin. Where to start with the troubleshooting? For all my sites and the hosting server I really don’t do much except keep the patches current and occasionally post content using WordPress CMS. Not much troubleshooting, monitoring logs, etc. because there isn’t much going on. And, though some might say otherwise, I don’t spend all my time at the computer dissecting how it operates.

I put off troubleshooting for a while. This web server’s experimental, not production, so sometimes I cut some slack and don’t dive right in when things aren’t working. Had other things pending that required more attention.

When I did start I was very much at a loss where to start because, as noted, I disabled a web site and everything continued to work for a while. When it stopped working I hadn’t made any additional changes.

Logs are always a good place to look, yes? This web server is set up to create separate logs for most of the sites it’s hosting. Two types of logs are created, access logs and error logs. Access logs showed what was expected, no more access to that site after I disabled it.

Error logs confused me though. The websites use Lets Encrypt SSL certificates. And they use Certbot to set up the https on the Apache http server. A very common setup. The confusing thing about the error log was it showed the SSL configuration for the expired web site failing to load. Why was the site trying to load at all??? I had disabled the site using the a2dissite program provided by the server distribution. The thing I hadn’t thought about is the Certbot script for Apache sets up the SSL by modifying the <site_name>.conf file AND creating a <site_name>-le-ssl.conf file.

So even though the site had been disabled by a2dissite <site_name>.conf I hadn’t thought to a2dissite <site_name>-le-ssl.conf. Once I recognized that issue and ran the second a2dissite command the web server again started right up. No more failing to load SSL for the expired site. And, surprising, failing to load the SSL for the one site prevented the server from starting rather than disabling the one site and loading the others that didn’t have configuration issues.

Something for another time… I expect there must be a way for the server to start and serve correctly configured sites while not loading incorrectly configured sites and not allowing presence of an incorrectly configured site to prevent all sites from loading. It just does not seem likely that such a widely used web server would fail to serve correctly configured sites when only one or some of multiple hosted sites is misconfigured.

The perils of part-time admin, or jack of all trades and master of none, is that these sort of gotcha’s pop up all the time because of limited exposure to the full breath of dependencies for a program to perform in a particular way. It isn’t a bad thing. Just something to be aware of so rather than blame the software for not doing something, need to be aware that there are often additional settings to make to achieve the desired effect.

Be patient. Expect to need to continue learning. And always, always, RTFM and any other supporting documents.

Virtual Host??

Setting up Apache to support multiple websites on one host. My server already does that for my public websites.

However I want to control what is returned to the browser if a site isn’t available for some reason. So I’ve set up a virtual server with multiple sites. Each site works when enabled. However if the site is set up to be unavailable, disabled, no index file, etc. the default page returned to the browser is not what I’d like.

Need to identify a few fail conditions, see what the server returns when the condition exists, see if what’s returned for a given condition is the same regardless which site the failure is generated by, then figure out why the webserver is sending back the page it does.

Reasons not available:

  • site not being served, e.g. not enabled on server
  • site setting wrong, e.g. DocumentRoot invalid
  • site content wrong, no index file

Answers that might be returned:

  • site not available
  • forbidden
  • …other’s I’ve seen but don’t remember now

From what I’ve read it seems whatever’s in 000-defalut.conf should control which page/site loads when a site isn’t available. That’s not the result I’m getting.

Either I’m doing it wrong or I’m just not understanding what’s supposed to happen and how to make it happen.

More digging…