Solving LinkDrama

The words "Links (and Posting!) All Working!" in white over an image of a mountain range

I think for now I’ve got the link drama solved. First, following Jason’s excellent advice, I changed the site URL in the settings. Then everything was working, except for the /blog/ was missing from blog posts.

I can either solve the problem from WordPress’ side, where I am familiar, or Gatsby’s side, where I am not so much.

Given those two options, I went with WordPress. The permalink settings saved the day. By adding the /blog/ to the permalink structure to produce /blog/%postname%/, I have solved all the problems. Since only posts, not pages, are affected by that change, I am good to go!

One interesting thing to note, though, and this may help others who come across this strange bug feature of the new Gutenberg editor (where I am happily writing this post!).

When you change the site and home URLs in WordPress, the Gutenberg editor stops working. That’s because Gutenberg uses the REST API to create posts, and there are some security features preventing access to files based on cross-site access.

Since it uses the site URL to build that URL for the REST request, it gets confused and doesn’t allow access to critical functionality like saving things, or adding featured images.

The fix is to add this bit of code (courtesy a user on the Gutenberg GitHub repo) to a must-use plugin (could also go in functions.php of the theme, but I like single-use must-use plugins for stuff like this):

add_filter('rest_url', function($url) {
    $url = str_replace(home_url(), site_url(), $url);
    return $url;
});

That fixes all my link problems! My site is now linking internally correctly. On to more learning!