WordPress Debugging

Sooner or later something will break on your site: a white page, a plugin error, or the message There has been a critical error on this website. WordPress has a built in debug mode which shows what is going wrong.

The debug constants

Add these lines in wp-config.php, above the line “That’s all, stop editing”:

define( 'WP_DEBUG', true );          // turn on debug mode
define( 'WP_DEBUG_LOG', true );      // save errors in wp-content/debug.log
define( 'WP_DEBUG_DISPLAY', false ); // do not show errors on the page
ConstantPurpose
WP_DEBUGMain switch of the debug mode
WP_DEBUG_LOGWrites the errors to wp-content/debug.log
WP_DEBUG_DISPLAYShows the errors on the page. Keep it false on a live site
SCRIPT_DEBUGLoads the not minified CSS and JS of WordPress
SAVEQUERIESSaves all database queries, so tools can show them

A real example

We create a small plugin with a mistake: it uses a variable which does not exist.

<?php
/**
 * Plugin Name: Buggy Demo
 */
add_action( 'wp_footer', function () {
	echo $total_price;              // this variable was never created
	error_log( 'Footer code ran' ); // our own message in the log
} );
Terminal output

$ wp plugin activate tc-buggy
Plugin 'tc-buggy' activated.
Success: Activated 1 of 1 plugins.
$ wp config set WP_DEBUG true –raw
Success: Added the constant 'WP_DEBUG' to the 'wp-config.php' file with the raw value 'true'.
$ wp config set WP_DEBUG_LOG true –raw
Success: Added the constant 'WP_DEBUG_LOG' to the 'wp-config.php' file with the raw value 'true'.
$ wp config set WP_DEBUG_DISPLAY false –raw
Success: Added the constant 'WP_DEBUG_DISPLAY' to the 'wp-config.php' file with the raw value 'false'.
$ curl -s -o /dev/null -w "%{http_code}\n" http://example.com/
200
$ cat wp-content/debug.log | sed 's/^\[[^]]*\] //'
PHP Warning: Undefined variable $total_price in /var/www/html/wp-content/plugins/tc-buggy/tc-buggy.php on line 6
Footer code ran

We removed the time at the start of each log line to keep the output short. The page still loads (code 200), but the problem is written in the log with the file name and the line number. Now we clean up the demo, because the debug mode must not stay on:

Terminal output

$ wp plugin deactivate tc-buggy
Plugin 'tc-buggy' deactivated.
Success: Deactivated 1 of 1 plugins.
$ wp config delete WP_DEBUG
Success: Deleted the constant 'WP_DEBUG' from the 'wp-config.php' file.
$ wp config delete WP_DEBUG_LOG
Success: Deleted the constant 'WP_DEBUG_LOG' from the 'wp-config.php' file.
$ wp config delete WP_DEBUG_DISPLAY
Success: Deleted the constant 'WP_DEBUG_DISPLAY' from the 'wp-config.php' file.
$ rm wp-content/debug.log

Write your own log messages

Use error_log() in your code. For arrays and objects use print_r with the second parameter true:

error_log( 'Order saved: ' . $order_id );
error_log( print_r( $data, true ) );

Fix the critical error

  • Open wp-content/debug.log (or the error log of your host) to see the file and the line.
  • If a plugin is the reason, rename its folder with FTP or the file manager (for example plugins/bad-plugin to plugins/bad-plugin-off). WordPress will deactivate it.
  • If the theme is the reason, rename the theme folder. WordPress will fall back to a default theme.
  • Check your PHP version. A new PHP version can break old code.
  • Use the recovery mode link which WordPress sends to the admin email.

Helpful tools

  • Query Monitor plugin: shows queries, hooks, errors and slow code.
  • Site Health (Tools > Site Health): checks versions, modules and common problems.
  • Your browser developer tools for JavaScript errors.
Note: Never keep WP_DEBUG_DISPLAY on a live site, it can show file paths to visitors. Also delete a big debug.log when you are done, and do not leave it open to the public. Next: WP-CLI Tutorial.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.