Enabling WP_DEBUG
When it comes to programming, its safe to say that no developer is perfect – you will write code that throws errors, and when you do you’ll need to know why. Even even the simplest syntax errors in WordPress theme code can give you a result that’s not particularly helpful – either the dreaded white-screen (no output), or a browser friendly error (friendly to website visitors, but certainly not developers). Consider the following code:
Example 1: A tiny mistake
Okay, you’re looking for an error, so you’ve probably spotted the mistake here fairly quickly – but dropping a semi-colon is a very easy thing to do. Unfortunately, this tiny error will either give us a “white screen” (absolutely no output), or a “friendly” error like this Chrome page:
If this is the only PHP code on your page, or you’ve only just added these 5 lines of code since last refreshing the browser, this might be easy to spot and fix. But if you’ve just written hundreds of lines of code including this error since you last refreshed, it could be far more difficult to discover the source of the problem.
The first thing you can do in this scenario is enable “WP_DEBUG” in your wp-config.php (WordPress configuration) file, by adding this line:
Example 2: The WP_DEBUG definition in wp-config.php
Now when you access the page with this code again, you’ll get something like thisĀ (you may also get other errors and warnings, but this would be in there somewhere):
Parse error: parse error, expecting `','' or `';'' in /path/to/your/site/wp-content/themes/my-theme/single.php on line 6
Much better – now you can quickly fix the error and move on. Enabling WP_DEBUG should often be your first port of call when you get a white-screen or friendly error.
Note that you should ensure WP_DEBUG is disabled on any production servers, or site visitors may see strange warnings and error messages. You can do this by either commenting this line of code out, or defining WP_DEBUG as false: