WordPress Debugging with WP_Debug and more

Home » Documentation » WordPress Debugging with WP_Debug and more

Debugging is an essential aspect of WordPress development. It helps identify and resolve issues in your code. This guide provides an in-depth explanation of four key debugging constants in WordPress: WP_DEBUG, WP_DEBUG_DISPLAY, WP_DEBUG_LOG, and SCRIPT_DEBUG.

These constants are set in your wp-config.php file, which is located in the root directory of your WordPress installation.

WP_DEBUG

WP_DEBUG is the cornerstone of WordPress debugging. It is a PHP constant that triggers the debug mode throughout WordPress. When enabled, it allows developers to see PHP errors, notices, and warnings, which are invaluable for troubleshooting.

Enabling WP_DEBUG

To enable WP_DEBUG, open your wp-config.php file and add the following line before the comment /* That's all, stop editing! Happy blogging. */:

define( 'WP_DEBUG', true );

By setting WP_DEBUG to true, you activate the debugging mode. This mode displays all PHP errors, notices, and warnings directly on the screen. This immediate feedback helps you identify issues such as undefined variables, deprecated functions, and syntax errors.

Example Scenario

Imagine you are developing a custom plugin, and something isn’t working as expected. Enabling WP_DEBUG will allow you to see any PHP errors that might be causing the issue. For example, you might see a notice like this:

Notice: Undefined variable: my_variable in /path/to/your/plugin/file.php on line 42

This notice tells you exactly what the problem is and where it occurs, making it easier to fix.

Best Practices

  • Development Environment: Always enable WP_DEBUG in your development environment to catch errors early.
  • Production Environment: Avoid enabling WP_DEBUG on live sites as it might expose sensitive information to visitors.

WP_DEBUG_DISPLAY

WP_DEBUG_DISPLAY works in conjunction with WP_DEBUG. It controls whether debug messages are displayed within the HTML of your pages or not. By default, it is set to true, meaning errors and warnings will be shown directly on the screen.

Disabling WP_DEBUG_DISPLAY

You can disable this behavior by adding the following line to your wp-config.php file:

define('WP_DEBUG_DISPLAY', false);

When WP_DEBUG_DISPLAY is set to false, debug messages are not shown on the screen but can still be logged if WP_DEBUG_LOG is enabled.

Example Scenario

Consider you have a live e-commerce site and you want to log errors for troubleshooting but don’t want your customers to see these error messages. By setting WP_DEBUG to true and WP_DEBUG_DISPLAY to false, you ensure that errors are logged but not displayed on the website. This way, you can review the logs without disrupting the user experience.

WP_DEBUG_LOG

WP_DEBUG_LOG directs all debug messages to a log file. When enabled, it writes these messages to a file named debug.log located in the wp-content directory. This is particularly useful for keeping a record of errors and warnings without displaying them on the screen.

Enabling WP_DEBUG_LOG

To enable logging, include this line in your wp-config.php file:

define( 'WP_DEBUG_LOG', true );

For more control, you can specify a custom log file path:

define( 'WP_DEBUG_LOG', '/path/to/your/debug.log' );

This feature is essential for tracking issues without displaying errors to users. It allows you to maintain a log of all debug messages, which can be reviewed later for troubleshooting.

Example Scenario

Let’s look at an example scenario where you are troubleshooting an intermittent issue that only occurs under specific conditions. By enabling WP_DEBUG_LOG, you can keep a continuous log of all errors and warnings. This log file can then be reviewed to identify the conditions under which the errors occur. For example, you might find entries like this in your log file:

[15-May-2023 10:15:24 UTC] PHP Notice:  Undefined variable: my_variable in /path/to/your/plugin/file.php on line 42

This entry provides a timestamp and the exact location of the error, making it easier to diagnose and fix the issue.

SCRIPT_DEBUG

SCRIPT_DEBUG forces WordPress to use the non-minified versions of CSS and JavaScript files. This is particularly useful during development when you need to read and debug the full, uncompressed source code.

Enabling SCRIPT_DEBUG

To enable SCRIPT_DEBUG, add this line to your wp-config.php file:

define('SCRIPT_DEBUG', true);

When SCRIPT_DEBUG is set to true, WordPress will load the full versions of CSS and JS files instead of the minified ones.

Example Scenario

Imagine you are developing a custom theme and facing issues with JavaScript not working as expected. By enabling SCRIPT_DEBUG, you can load the full, non-minified versions of your JavaScript files. This makes it easier to read the code and identify the exact line causing the issue. For example, you might find a syntax error in the full version of your JavaScript file that is not apparent in the minified version.

Best Practices for Production vs. Development Environments

Understanding which constants to enable in different environments is crucial for maintaining site security and performance.

Development Environment

In a development environment, you want to see all errors and warnings and log them as well:

define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', true);
define('WP_DEBUG_LOG', true);
define('SCRIPT_DEBUG', true);

Production Environment

In a production environment, you want to log errors without displaying them to users:

define('WP_DEBUG', true);
define('WP_DEBUG_DISPLAY', false);
define('WP_DEBUG_LOG', true);
define('SCRIPT_DEBUG', false);

Constants to Avoid in Production

  • WP_DEBUG: While it can be useful in production under controlled circumstances, it is generally safer to disable it to avoid exposing sensitive information.
  • WP_DEBUG_DISPLAY: Should always be set to false in production to prevent error messages from being displayed to users.
  • SCRIPT_DEBUG: This should be disabled in production to ensure that minified versions of scripts are used, which improves the frontend performance of your WordPress site.

As a general note on performance, it’s also good to understand that having these constants active in production is a relatively expensive thing to add to your codebase. Meaning, running these instants requires quite a bit more resources.

Constants Safe for Production

  • WP_DEBUG_LOG: This can be safely used in production to log errors without displaying them to users. However, ensure that the log file is not publicly accessible.

Advanced Debugging

While you’re debugging, there are a few more options available to you outside of the abovementioned WordPress Constants. Be sure to include the following as well:

  • Error Reporting Levels: You can control the level of error reporting using PHP’s error_reporting() function. For example: error_reporting(E_ALL); This line will report all types of PHP errors.
  • Custom Error Handlers: You can create custom error handlers to manage how errors are logged or displayed. For example: set_error_handler('my_custom_error_handler'); function my_custom_error_handler($errno, $errstr, $errfile, $errline) { // Custom logic for handling errors }
  • Debugging Plugins: Consider using debugging plugins like Query Monitor or Debug Bar. These plugins provide a user-friendly interface for viewing debug information directly within the WordPress admin panel.

In summary, WordPress provides powerful debugging tools that can help you identify and resolve issues in your code. WP_DEBUG is the primary switch for debugging mode, while WP_DEBUG_DISPLAY and WP_DEBUG_LOG offer control over how and where errors are shown or logged. SCRIPT_DEBUG ensures you work with non-minified scripts for easier debugging.

By understanding and utilizing these constants, you can create a more efficient development workflow. This results in higher-quality code and a more stable WordPress site. Use these tools to create a more robust and error-free WordPress site.