How to Display Error Messages in WordPress (Using Debug Mode)

When developing or troubleshooting a WordPress site, it’s often necessary to see detailed error messages and warnings to understand what’s going wrong. WordPress provides a built-in debugging feature that allows you to see these messages by setting certain constants and ini settings. By enabling debugging mode, you can see any errors or warnings generated by your code, log them to a file, display them on the screen, use non-minified versions of core files, and save all database queries.

Additionally, some PHP ini settings also can be set to display errors on screen and set the error reporting level. These settings are useful for debugging on a local development environment or on a staging site, but should not be used on a live site as they may reveal sensitive information about your code and server.

/* add in config file  */
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', true );
define( 'SCRIPT_DEBUG', true );
define( 'SAVEQUERIES', true );

/* add in function.php file  */

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

The above code enables WordPress debugging by setting several constants and ini settings.

  • define( 'WP_DEBUG', true ); – This constant enables WordPress debugging mode which allows you to see any errors or warnings generated by your code.
  • define( 'WP_DEBUG_LOG', true ); – This constant tells WordPress to log the errors and warnings to a file called debug.log in the wp-content directory. This allows you to see the errors at a later time or when you don’t have access to the site’s front-end.
  • define( 'WP_DEBUG_DISPLAY', true ); – This constant tells WordPress to display errors and warnings on the screen. This is useful for debugging on a local development environment or on a staging site.
  • define( 'SCRIPT_DEBUG', true ); – This constant tells WordPress to use the non-minified version of core JavaScript and CSS files. This is useful for debugging as it makes it easier to understand the error messages.
  • define( 'SAVEQUERIES', true ); – This constant tells WordPress to save all database queries to an array and make it available through the global $wpdb->queries variable. This is useful for debugging and optimizing database performance.
  • ini_set('display_errors', 1); – This ini setting tells PHP to display errors on screen.
  • ini_set('display_startup_errors', 1); – This ini setting tells PHP to display startup errors on screen.
  • error_reporting(E_ALL); – This function sets the error reporting level to E_ALL, which will report all errors, warnings, and notices.