Dealing with load-scripts.php console errors in WordPress

I recently encountered an issue that was driving me completely nuts, so I thought I’d do a short write up on it to see if it helps anyone else.

The Problem

Every time I would visit the Edit Post page in the WordPress Dashboard and tried to do things like adding a category (or some other thing that uses JavaScript/AJAX) I’d get a console error that referenced load-scripts.php. This is a core WordPress file that I naturally had not edited in any way and I didn’t even have any sort of customizations in place that affect the dashboard. I only had two clues:

  1. The site I was working on had recently been cleaned of a virus
  2. The issue was only caused by my theme (i.e. activating Twenty-Seventeen cleared up the console error)

What I tried

The first thing I thought of was to double-check the remote theme files to be sure there wasn’t still some malicious code or a new file present that may be causing the conflict. I checked this and found none of the theme files contained any malicious code and no new files had been added. Beyond that, I tried disabling some plugins and experimenting with some active, some disabled. Unfortunately, none of these things yielded any results.

The Solution

Ultimately the issue came down to extra white-space in the top of a functions.php include. If your WordPress theme’s functions.php file (or any files included/required inside of it) has extra white-space before the first opening PHP tag, WordPress may not function correctly. The exact issue(s) caused by the white-space varies depending on your setup and in this case, it took the form of a console error.

The moral of the story is: if you have some inexplicable error that’s related to your specific theme and you can confirm you don’t have a virus, double-check for the presence of extra white-space in your theme’s files. It is also worth noting that white-space in plugin files can also be problematic. However, if you’re using paid plugins or plugins from the WordPress theme repository, the likelihood you’ll encounter this is relatively low, especially if you’re using popular, battle-tested plugins.