If you are facing issues with your WordPress site, like a white screen of death displayed when errors are reached, enabling debugging to figure out what is wrong is a good idea. When debugging is enabled in WordPress, detailed error messages are displayed, and you can also write them in a separate log file.
Importance of WP_DEBUG mode
Debugging is an important process for any site because it helps identify bugs and optimize performance. Debugging can be achieved with WP_DEBUG mode. There are many advantages to using WP_DEBUG, so this is useful not just for developers but also for beginners who want to diagnose errors in their code. From the perspective of a beginning site builder, debugging can help them find out what’s going on inside WP_DEBUG mode. This can be useful for tracking down bugs on their site.
Setup Debugging in WordPress
To enable debugging in WordPress, please follow the process below:
Locating the wp-config.php file
The wp_config file is located in the root of your WordPress file directory and contains your website’s base configuration details, such as database connection information. You can access this using FTP or cPanel.
Editing the wp-config.php file
Open the wp_config.php file and then try to locate a specific comment /* That’s all; stop editing! Happy blogging. */ “. Above this, you have to enter a small code to enable debugging in WordPress
Adding WP_DEBUG code
WP_DEBUG
To enable debug mode, set the WP_DEBUG constant to true. The value of wp_debug is false by default. Write the following code in the config file.
// Enable WP_DEBUG mode
define('WP_DEBUG', true);
Now check your website, and if any errors are being generated, they will be displayed at the top of your page.
WP_DEBUG_LOG
Write error messages to the log file. If you want to save the errors generated by the WP_DEBUG in a separate file for later analysis, write the code below in thewp-config.php file.
// Enable Debug logging to the /wp-content/debug.log file
define('WP_DEBUG_LOG', true);
A log file named debug.log is generated in the wp-content directory.
Note: For WP_DEBUG_LOG to do anything, WP_DEBUG must be enabled (true).
Disable debug info from displaying on the frontend
It controls whether to display debug messages inside the HTML of the page or not. To review errors later use WP_DEBUG_DISPLAY in conjunction with the WP_DEBUG_LOG.
Write the following code in the wp_config file to display the error messages.
// Disable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
Note: For WP_DEBUG_DISPLAY to do anything, WP_DEBUG must be enabled (true).
SCRIPT_DEBUG
SCRIPT_DEBUG
will force WordPress to use the “dev” versions of core CSS and JavaScript files rather than the minified versions that are normally loaded.
// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );
SAVE QUERIES
The SAVEQUERIES
saves the database queries to an array and that array can be displayed to help analyze those queries.
define( 'SAVEQUERIES', true );
The array is stored in the global $wpdb->queries
.
NOTE: This will impact your site’s performance, so make sure to turn this off when you aren’t debugging.
Checkout: 8 Useful Tips for WordPress Security
What is the difference between Testing and Debugging?
The difference between testing and debugging is that testing looks for errors, and debugging tries to find the source of the error. Testing is usually done on a new website before it’s launched while debugging is done on a website that already exists.
Wrap Up
WP_DEBUG displays all PHP notices, warnings, and errors on your site. It also gives you the time it took to retrieve your page’s content, the performance statistics of your website, and the exact line numbers of WordPress’ core files where errors occurred.