Debugging in WordPress

Any code developed for the first time is not 100% bug free. While working on a WordPress based website or any other technology website it becomes easy to fix the bugs/issues if they are pointed out by the system.

“wp-config.php”  file located at the root of wordpress folder provides settings that enable the “Debugging” mode in wordpress.

The following code, inserted in your wp-config.php file will log all errors notices and warnings to a file called debug.log in the wp-content directory.

// Enable WP_DEBUG mode
define('WP_DEBUG', true);

// Enable Debug logging to the /wp-content/debug.log file
define('WP_DEBUG_LOG', true);

// Disable display of errors and warnings
define('WP_DEBUG_DISPLAY', false);

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define('SCRIPT_DEBUG', true);

Let’s have a look at the above definitions individually:

  • define(‘WP_DEBUG’, true) : Enabling ‘WP_DEBUG’ will cause all PHP errors, notices and warnings to be displayed.
  • define(‘WP_DEBUG_LOG’, true) : Adding this setting will cause all errors to be written to a debug.log log file inside the /wp-content/ directory.
  •  (‘WP_DEBUG_DISPLAY’, false): Setting ‘WP_DEBUG_DISPLAY’ to false will prevent error from being displayed on the page, it should be used with setting “define(‘WP_DEBUG_LOG’, true)” so that errors are written to a file else you may set ‘WP_DEBUG_DISPLAY’ to true so that errors are displayed on the page itself.
  •  define(‘SCRIPT_DEBUG’, true): This setting will force WordPress to use the “dev” versions of core CSS and Javascript files rather than the minified versions that are normally loaded. This is useful when you are testing modifications to any built-in .js or .css files.

In case you need to analyse database queries you can add the following in wp-config.php file.

define(‘SAVEQUERIES’, true);

This setting causes the database queries to be saved to an array. Later this array can be displayed to help analyze those queries. The information saves each query, what function called it, and how long that query took to execute.

NOTE: Do not forget to disable the “DEBUG” mode when making the website live as it may be a security risk.

Leave a Comment

Back to top