Debugging In WordPress

Debugging is a part of any developer’s life. But debugging in the absence of a supporting environment is a developer’s nightmare. Thankfully, WordPress provides efficient debugging mechanisms to simplify the process and also to standardize code across the core, themes, and plugins. This article describes the various debugging tools provided by WordPress that will help you increase your quality and productivity.

WP_DEBUG

If you are a WordPress user, chances are that you will have encountered the White Screen of Death. It is nothing but a blank screen with no information that is displayed when WordPress finds PHP or database errors. WordPress provides a constant, WP_DEBUG, which enables all PHP notices to be displayed when set to true. WordPress will also display notices related to deprecated functions, function arguments, or files. This is very useful because deprecated code is unreliable and may be removed in future versions of WordPress. It is strongly recommended that plugin and theme developers enable WP_DEBUG in their development environments.

WP_DEBUG is set to false by default. You can enable the WP_DEBUG constant by adding the following line in your wp-config.php file:

define(‘WP_DEBUG’, true);

WP_DEBUG_LOG

Wouldn’t it be helpful to have all the errors and notices to be logged to a file so that you can view them at your leisure? The WP_DEBUG_LOG constant achieves this by saving all errors to a log file, /wp-content/debug.log . It defaults to false and can be enabled by adding the following line to your wp-config.php file:

define(‘WP_DEBUG_LOG’, true);

Note: WP_DEBUG_LOG, even when enabled, performs no function until WP_DEBUG is set to true.

A snippet of debug.log file is as follows:

WP_DEBUG_DISPLAY

The constant WP_DEBUG_DISPLAY controls whether debug messages are displayed as they are generated. It defaults to true and setting it to false will hide all errors. It should be used along with WP_DEBUG_LOG so that all the errors can be logged to a file. You can disable this constant by adding the following line to your wp-config.php file:

define(‘WP_DEBUG_DISPLAY’, false);

Note: WP_DEBUG_DISPLAY, even when enabled, performs no function until WP_DEBUG is set to true.

SCRIPT DEBUG

WordPress minifies JavaScript and CSS scripts to reduce their file size. You can force WordPress to use the complete versions of these files by setting the SCRIPT_DEBUG constant to true. This feature is useful when you want to test any changes made to the JavaScript and CSS files. The SCRIPT_DEBUG constant defaults to false. You can enable it by adding the following line to your wp-config.php file.

define(‘SCRIPT_DEBUG’, true);

SAVEQUERIES

WordPress, as you know, stores all your website content in a database. Hence it is required to query the database in order to access the information stored in it. While debugging database errors, it may be useful to have a list of these queries available to you. This can be achieved using the SAVEQUERIES constant. When set to true, each query is saved to an array along with the execution time and the function which called it. This array is stored in the global $wpdb->queries. It defaults to false and you can enable it by adding the following line to your wp-config.php file:

define(‘SAVEQUERIES’, true);

A Sample Configuration File

To enable debugging in WordPress, the following lines need to be added. The result is that all errors and notices will be logged to the /wp-content/debug.log file.

Plugins

There are many plugins that make debugging WordPress really easy for you, Debug Bar is one such plugin.