Debugging WordPress: How to Use WP_DEBUG

Debugging WordPress
Debugging WordPress

Debugging WordPress

Every good developer should turn on debugging before getting started on a new plugin or theme. In fact, the WordPress Codex “highly recommends” that developers useWP_DEBUG mode while working on code they plan to release publicly. Debugging WordPress..

Unfortunately, many developers don’t follow this advice simply because they forget about the debugging tools built-in to WordPress or just don’t know how to use them.

In this post I’ll go through the basic debugging tools available and show you how easy they are to use.

Debugging With WP_DEBUG

The most important debugging tool you need to know about is WP_DEBUG.

WP_DEBUG is a boolean constant, which triggers the “debug” mode throughout WordPress. It’s found in the wp-config.php file in your WordPress install.

When set to “true,” you’ll start seeing PHP notices – not so much errors as they are helpful messages for developers – and WordPress-generated debug messages, particularly deprecated function usage, displayed on your site’s pages.

Deprecated functions, which are contained in many WordPress releases, are functions that have been flagged to die at a later date. It’s important to know if you are using a deprecated function in a theme or plugin you’re working on so you can find a replacement to use instead.

To turn on WP_DEBUG, simply add the following line of code to your wp-config.php file:

To turn the constant off, just replace “true” with “false”. WP_DEBUG is set to false by default.

WP_DEBUG provides a handy way to troubleshoot problems when something goes wrong with your site. Debugging WordPress.

It’s important to keep in mind that WP_DEBUG should not be used on a live site. While it’s a useful feature during development, it can be dangerous on a live site because text in the PHP notices can reveal details about your code, paths and other information to visitors to your site.

Logging Errors With WP_DEBUG_LOG

Another handy tool is WP_DEBUG_LOG, which can be used in conjunction withWP_DEBUG to save all error messages to a debug.log file located in the /wp-content/ directory of your site.

This is an especially useful feature if you want to review notices later.

To turn on debug logging, simply add the following line of code to your wp-config.php file:

Turn Off Displaying Errors On Your Site With WP_DEBUG_DISPLAY

If you don’t want error messages published to your site’s pages you should useWP_DEBUG_DISPLAY.

This is another useful constant, which allows you to control whether debug messages are shown inside the HTML of your site.

The default is “true,” which shows errors and warnings as they are generated. Changing this to “false” will hide all errors. Debugging WordPress.

This constant should be used in conjunction with WP_DEBUG_LOG.

To use his feature, just add the following line of code to your wp-config.php file:

Putting it All Together

It’s easy to use all three constants together if you want to turn debugging on and log error messages but hide notices displayed on your site:

Don’t forget that WP_DEBUG is for local development use and should not be used on live sites.