Find and fix errors in WordPress with debugging

Software isn’t perfect and sometimes things don’t go to plan. With code changes and as new themes or plugins are either installed or updated, there’s always the possibility that you’ll have to do some digging when there’s a failure or error of some sort on your website.

Debugging to the rescue! If you aren’t familiar with debugging, it’s a lot like a treasure hunt or murder mystery. Paired with your ability to look for clues, debugging is a combination of things you use and do to uncover the tracks that caused your website to stop working in the way you’d expect it to. Debugging is also commonly enabled when developing or testing to spot potential problems with custom code or setting up new plugins.

Before getting started, it’s always a good idea to take a backup of your site and spin up a version of your website to test changes without affecting the users to your site.

To enable debugging, you have the pick of debugging plugins from the WordPress repository or adding code to the wp-config.php file on your site.

If you prefer wrangling code over plugins, you can set the ‘WP_DEBUG’ constant to true in the wp-config.php file just before /* That’s all, stop editing! Happy publishing. */. Once it’s turned on, it displays notices, warnings or errors on the screen.

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

Notices and warnings aren’t always a bad sign. They may appear because they are needed to maintain compatibility with older versions of software and are likely to disappear in future releases.

Assuming you want enable debugging on your live site and you don’t want users to see all the notices, warnings or errors, you can add the constant ‘WP_DEBUG_DISPLAY’ and set it to false.

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

There’s also the option to log all the details in a file by adding the constant ‘WP_DEBUG_LOG’. This is useful if you want to go through them later or off-screen. By default, ‘WP_DEBUG_LOG’ saves the file in the /wp-content/ folder on your site as debug.log.

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

Pro tip

If debugging is turned on a live site, the debug.log file will be accessible to anyone on the internet. A malicious user could use the information in the file to carry out attacks on your site. Consider restricting access to the log file, saving it to a secure location or disabling debugging on a live site.

You can tweak the ‘WP_DEBUG_LOG’ constant if you’d like to save the details to a different location and file.

// Enable debug logging to the /tmp/ folder and wp-errors.log file
define( 'WP_DEBUG_LOG', '/tmp/wp-errors.log' );

Bringing it all together, add the code below to your wp-config.php file. Log files can get large very quickly so make sure there’s enough storage either on your device or your web host and remember to disable logging once you are done. It’s also safe to delete debug.log if it’s no longer needed.

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

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

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