When writing PHP for ClassicPress plugins or themes, you should always enable error reporting. This allows you to catch and solve problems before releasing the code. The bad thing about error messages, though, is that they are sometimes a cryptic garble of text.
Like many, I use XAMPP as a local development environment (Apache server) and I have a tip for you that will help immensely! You can improve the readability of error messages with just a tiny bit of CSS – yes, CSS! Alright, take a quick look at the side-by-side image…then, let’s do this thing!
Enable Debugging in ClassicPress
Before you can see errors on the screen, you’ll have to enable debugging mode in ClassicPress. This is achieved by making a simple change in the
wp-config.php file. Do this first.
- In `wp-config.php`, find the line that reads
- Change the `false` to
- Save the file.
That takes care of enabling error reporting for ClassicPress. Next, let’s take a look at the default display for error messages to get a baseline for comparison.
Default PHP Error Message Display
For the following screen capture, I’ve written PHP that generates one notice, one warning, and one fatal error. Since the notice and warning only span a single line, they are fairly easy to understand. However, the fatal error, which has produced a stack trace, is much harder to understand.
Improved PHP Error Message Display
The screen capture below shows vastly improved readability. Most notably, the stack trace is actually readable. So, how can you fix your error display? Glad you asked!
How to Apply CSS to the PHP Error Display
You will be adding your CSS to the
php.ini file, so, the first thing to do is open it for editing. If you’re also using XAMPP, you can quickly locate and open the file using the XAMPP Control Panel.
- Open your `php.ini` file in any text editor.
- Search for `error_prepend_string`:
The default value is something like
<span style='color:#FF0000;'>. Change it to
- Search for `error_append_string`:
The default value is a closing
</span>tag. Change this value to closing `</div>` tag.
- Save the `php.ini` file.
- Stop and restart Apache.
It really is that simple. After adding the above tweaks to your
php.ini file, debugging will be faster and easier. The error messages are enhanced with style and readability! Now, to satisfy the curiosity of anyone who might wonder, let me leave you with this:
XAMPP stands for cross-platform (X), Apache server (A), Maria Database (M), PHP (P), and Perl (P). Some people say XAMPP like “zamp” while others say it like “eggs-amp” – either way, they’re talking about the same thing.
What do you think?
Did you know PHP errors were so easy to reformat? How will you style your display? Or, are you a ninja that can grok a stack trace in your sleep?! I’d love to hear your thoughts – let me know in the comments!