Enable Debug Mode In WordPress

How To Enable Debug Mode In WordPress

Encountering issues on your WordPress site can be frustrating. Fortunately, WordPress includes a built-in tool known as Debug Mode that can help you identify and solve problems more effectively. This guide will walk you through the simple steps to enable Debug Mode, providing you with a powerful resource to troubleshoot errors, warnings, and notices within your site's code. Whether you're a beginner or an experienced developer, understanding how to leverage this functionality can significantly streamline your debugging process.

If your looking to elevate your site with a custom plugin that does exactly what you need.
Contact us to start your custom plugin project today!

How To Enable Debug Mode In WordPress

Enable Debug Mode In Wp-Config

Find wp-config.php in the main wordpress directory and change it to true.

wp-config.php

// Enable WP_DEBUG mode
define( 'WP_DEBUG', true );
This line activates WordPress's debugging mode. WP_DEBUG is a PHP constant that, when set to true, turns on the reporting of PHP errors, notices, and warnings. This is particularly useful during development to catch potential issues in the code. Remember, it should be set to false on a live site to prevent the display of sensitive information to visitors.

Enable Debug Logging & Disable Front End Messages

wp-config.php

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );
By setting this constant to true, WordPress will log any errors, warnings, or notices into a file named debug.log within the wp-content directory. This provides a secure way to store error messages without displaying them to site visitors, allowing developers to review and address issues as needed without compromising the user experience or site security.

wp-config.php

// Disable display of errors and warnings on the front end
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
WP_DEBUG_DISPLAY controls whether debug messages are shown within the HTML of pages or not. Setting this to false ensures that these messages are not displayed on your live site, which is crucial for maintaining a professional appearance and safeguarding information. The @ini_set('display_errors', 0); line reinforces this setting by directly influencing the PHP configuration, ensuring that even if other settings fail, error messages will still not be displayed.

Enable SAVE QUERIES To Analyse Database Queries

The SAVEQUERIES constant in WordPress is used to save database queries for analysis, which can be incredibly useful for debugging and optimizing your website. When you define SAVEQUERIES as true, WordPress stores an array of every database query made during the page request, along with information about how long each query took to execute and the function that called it.

wp-config.php

define( 'SAVEQUERIES', true );
Accessing Query Data: You can access this data through the global $wpdb object. For example, you can output or log the contents of $wpdb->queries to review the queries. This is especially useful in a development environment to identify slow queries or inefficiencies in how queries are constructed. Add the following PHP code to a custom plugin, or your theme’s functions.php file. This code snippet will output all the queries executed during the page load, along with execution times and the stack trace of their origins:

functions.php

add_action('shutdown', function() {
    global $wpdb;

    if (defined('SAVEQUERIES') && SAVEQUERIES) {
        echo '<pre>';
        print_r(array_map(function($query) {
            return [
                'query' => $query[0],
                'execution_time' => $query[1],
                'call_stack' => $query[2],
            ];
        }, $wpdb->queries));
        echo '</pre>';
    }
});

Enable SCRIPT_DEBUG

In WordPress, the SCRIPT_DEBUG constant is a useful tool for developers who need to work with the uncompressed versions of JavaScript and CSS files typically used by WordPress core, plugins, and themes. Enabling this constant can be particularly helpful for debugging and development purposes. When you define SCRIPT_DEBUG as true, WordPress bypasses the minified or concatenated versions of JavaScript and CSS files that are often used on live sites to improve load times and reduces file sizes. Instead, it loads the full, uncompressed, and non-minified versions of these files. This allows developers to see and debug the original, more readable source code.

wp-config.php

define( 'SCRIPT_DEBUG', true );

Writing Your Own Error Logs in WordPress

In WordPress, the error_log() function is an invaluable tool for developers looking to track custom events or errors within their code. This PHP function allows you to send error messages directly to the server's error log or to a file of your choice, depending on your server configuration. To log a simple message, you can use the error_log() function as follows:

website-custom-features.php

error_log('Website Custom Features Activated!');

Customizing the Log Destination:

If you want to direct your log messages to a specific file, you can specify the file path as a second argument. In this example, the 3 indicates that the message should be appended to a file, and the path the file to which the message will be written.

website-custom-features.php

error_log('Website Custom Features Activated!', 3, '/path/to/your/custom.log');

Examples of When to Write Custom Logs

Error Handling: Log specific errors that are critical for understanding the issues users might encounter. Debugging: During development, log important variables or states that could help in debugging.

website-custom-features.php

// Error Handling
if ($error_condition) {
    error_log('Error encountered: Description of the error');
}

// Log important variables or states that could help in debugging.
error_log('Post data: ' . print_r($_POST, true));

WordPress Debug Plugins

Debug plugins for WordPress, like the Debug Bar, are essential tools that enhance the debugging capabilities of a WordPress installation. These plugins provide a more interactive and user-friendly interface for identifying and resolving issues within your website. Here’s more about the Debug Bar and some other popular debugging plugins you might consider.

Debug Bar:

Debug Bar is a plugin that adds a debug menu to the WordPress admin bar. This menu provides detailed insights into SQL queries, caching, and other performance-related information, which can be critical for optimizing your website.

When WP_DEBUG is enabled, Debug Bar extends its functionality by tracking and logging PHP warnings and notices. This makes it easier for developers to spot and fix problems without scouring through log files.

It’s particularly useful during development or for ongoing maintenance of live sites where performance tuning is necessary. The insights provided can help developers make informed decisions about optimizations to reduce load times and improve overall site performance.

Query Monitor:

This plugin offers comprehensive information about database queries, hooks, conditionals, HTTP API calls, and more. It’s a more advanced tool that can help developers dig deep into WordPress core, themes, and plugins to find the underlying cause of performance issues.

Log Deprecated Notices

This plugin is especially useful when upgrading WordPress core, themes, or plugins. It logs the use of deprecated files, functions, and function arguments, giving developers alerts so that they can replace outdated code and maintain compatibility with newer versions of WordPress.

Simply Show Hooks

If you’re trying to understand what’s happening where in WordPress, this plugin can help by showing you all the hooks that are firing on the current page. It’s invaluable for developers who are creating or modifying themes or plugins and need to attach their functions to specific hooks.

WP Crontrol

WP Crontrol lets you view and control what’s happening with the WP-Cron system, which is used to schedule tasks in WordPress. This plugin allows you to edit, delete, and add custom cron jobs which can be crucial for timing events or tasks within WordPress.

WordPress Debug Mode Best Practices

Security Considerations:

Always ensure that debugging is turned off on a production site. Exposing error messages publicly can reveal underlying issues with your site, potentially giving attackers a way to exploit vulnerabilities.

It's best to use these tools during the development phase of your website or when conducting performance audits on a staging site. Avoid running these tools on a production site unless necessary as they can introduce overhead and potentially expose sensitive information.

Logging Best Practices:

Regularly monitor your debug.log file and address the issues logged there. This can help in maintaining the health and performance of your site. Also, secure access to your log files to ensure that sensitive information is not accessible to unauthorized users.

Check out our FREE ONLINE
WordPress Plugin Development Course

Looking to develop a custom plugin, or create new features for your website?

Facebook Video