15 Jul WordPress Error Log: How to Use WP_DEBUG
Hey developer. Started work on that next WordPress plugin or theme yet? Putting together your tools? I see you’ve already set up your local development environment, but is it really ready to go? I mean, have you turned on WP_DEBUG, and if you haven’t, any good reason why you’re developing your themes and plugins with WP_DEBUG turned off?
Many questions I know, but if you have not been using WP_DEBUG, you shouldn’t develop for the general public. No, I’m not trying to show you how to do your job, but WordPress highly recommends using WP_DEBUG if you plan to release code to the public.
In this quick post, we will talk about the basic debugging tools available in WordPress, so you can build great plugins and themes that pass WordPress’ rigorous review checks.
According to WordPress, WP_DEBUG is nothing more than a permanent global variable, also known as a PHP constant, that you use to turn on/off debugging mode in WordPress. By default, the debug mode is turned off, but you’re encouraged to turn it on when starting work on a new WordPress theme or plugin. Oh by the way, WP_DEBUG was introduced in WordPress version 2.3.1.
Get 20% off on the Best WordPress Themes & Plugins
You can trigger WP_DEBUG in the wp-config.php files that lives in your WordPress directory. It is easy work, just set WP_DEBUG to true or false to turn the debugging mode on or off respectively.
To turn on WP_DEBUG, just add the following code to your wp-config.php file:
define ( 'WP_DEBUG', true);
To turn the debugging mode off, replace the above code with:
define ( 'WP_DEBUG', false);
When WP_DEBUG is turned on, PHP notices and debug messages will appear on your website’s pages. These messages usually help other developers like you to troubleshoot problems with their code. In a nutshell, turning WP_DEBUG on (setting to true) causes all PHP warnings, notices, and errors to be displayed.
But turning on WP_DEBUG only isn’t enough. You need to keep logs of the errors to review later, which introduces us to our next feature.
To save your errors and notices to a log file, you just need to turn on the WP_DEBUG_LOG feature. When you set it to true, this nifty tool will save all errors to a debug.log file that is then stored in your /wp-content/ folder.
To turn WP_DEBUG_LOG on, just add the following code to your wp-config.php file:
define ( 'WP_DEBUG_LOG', true);
Now your WordPress installation will save a log of all errors to a debug.log file. Seeing that we can save the errors, we don’t need them to appear on your site’s pages. This is where another PHP constant, WP_DEBUG_DISPLAY comes in.
To prevent the error messages from showing on your pages as they’re generated, you just need to turn off the WP_DEBUG_DISPLAY constant. This way, the PHP errors won’t be shown inside the HTML of your site.
How do you turn off WP_DEBUG_DISPLAY? Simply add the following code to your wp-config.php:
define ( 'WP_DEBUG_DISPLAY', false);
To turn on debugging and log error messages but hide notices on your site, you just need this code:
// Turn WP_DEBUG on define ( 'WP_DEBUG', true); // Save errors to /wp-content/debug.log define ( 'WP_DEBUG_LOG', true); // Hide errors define ( 'WP_DEBUG_DISPLAY', false);
We hope this introduction to WP_DEBUG has shown you how to use this overlooked feature to log errors and debug your site. Don’t forget it’s not recommended to use WP_DEBUG on a live site, for obvious security reasons.
Do you turn on WP_DEBUG when developing with WordPress, or do you forget?