When it comes to developing on a new project one of the most important steps you need to setup is how you can successfully debug the application, WordPress is no different and comes with a number of debug settings to customise how you want the debug messages to be displayed.
Here we're going to go through these settings on how we can customise WordPress debug how we want it using the wp-config file.
Turn On WordPress Debug
The first step to debug with WordPress is to add a new constant to your wp-config.php file for WP_DEBUG, this will make WordPress output any error messages.
define( 'WP_DEBUG', true );
The default to WP_DEBUG is set to false, it is advised to set this to true only in staging and development environments. As this is placed inside your wp-config.php you can provide different functionality on different environments by using the SERVER['server_name'] variable.
switch( strtolower($_SERVER['SERVER_NAME']) )
{
case 'dev':
define( 'WP_DEBUG', true );
break;
default:
define( 'WP_DEBUG', false );
break;
}
When WP_DEBUG is set to true it will make all PHP warnings, notices and errors be displayed, seeing these errors makes it a lot easier to fix the problems in a development environment.
WordPress Debug Log
When WP_DEBUG is turned on you can also choose if you want these errors to be stored inside the WP_DEBUG log this is located in a file called to a debug.log file inside wp-content directory.
This is useful if you are having testers on your site and you want to review what errors they were facing or if you want to view these errors at a later time.
To turn on the debug log file you first need to turn WP_DEBUG on then you can add the debug log to the wp-config.php.
define( 'WP_DEBUG_LOG', true );
Display WordPress Debug
Along with outputting the debug messages to the log you can decide if you want to output the debug messages to the screen by using the constant WP_DEBUG_DISPLAY.
define( 'WP_DEBUG_DISPLAY', true );
This means you can now stop the debug messages being displayed on the screen but can place them in the log file so you can view them later, this will be perfect if you're in UAT testing with your clients, hide the errors but add them to the log file for you to deal with at a later time.
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
WordPress Script Debug
By default WordPress will use the minified versions of CSS and JS files, if you want to debug anything WordPress is doing with styling or Javascript it's easier to use the full version of these scripts. Adding the constant SCRIPT_DEBUG will tell WordPress to use the full version of the files instead of the minified versions.
define( 'SCRIPT_DEBUG', true );
Save Queries
If you need to debug database queries and how long they're taking you can use a WordPress SAVEQUERIES.
define( 'SAVEQUERIES', true );
This will store the queries to a database array which can accessed from $wpdb->queries.
var_dump( $wpdb->queries );
It's worth noting that will have a big impact on performance and therefore should only be used when you need to debug database queries and then remember to turn it off when you're not using it.
WP-Config Generator
To see what other settings you use with WordPress on the wp-config file try out wp-config Generator.