Enjoy Up To
80% OFF

How to enable WordPress Debug log

How to enable WordPress Debug log

WP DEBUG is a permanent global variable in PHP that can be used to activate the “debug” mode in WordPress. It is assumed to be false by default, but on development copies of WordPress, it is usually set to true in the wp-config.php file.

When enabled, WordPress debug will log any errors detected on your site. This can be key to finding the source of an issue or just learning more details about any possible errors on your site.

In this blog, we’ll discuss how to enable WordPress to debug logs via wp-config.php file edit.

Why need to enable debug log on WordPress?

If you can display your WordPress errors, must enable debug logs. If you enable a debug log option, then WordPress will generate error logs and display error details on your website view page.

You’re a WordPress Developer and can develop your WordPress site, you can enable debug mode for finding errors.

How to enable debug mode using the wp-config file?

First, open your wp-config.php file in any text editor to edit this file and follow some steps.

Enable debugging:

define( 'WP_DEBUG', true );

Disable debugging:

define( 'WP_DEBUG', false );

How to display error logs on the WordPress site?

WP DEBUG LOG and WP DEBUG DISPLAY are PHP constants that extend WP DEBUG and can be used to debug WordPress as well.

You’ll also discuss, in this section, how to enable SCRIPT_DEBUG to display JavaScript errors.

WP_DEBUG_LOG:

WP DEBUG LOG is a plugin that works with WP DEBUG to save all errors to a debug.log log file in the /wp-content/ directory.

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', false );

Copy and paste this code following the screenshot.

Enable debug log:

define( 'WP_DEBUG_LOG', true );

-or-

define( 'WP_DEBUG_LOG', '/tmp/wp-errors.log' );

Note: for WP_DEBUG_LOG to do anything, WP_DEBUG must be enabled (true). Remember, you can turn off WP_DEBUG_DISPLAY independently.

WP_DEBUG_DISPLAY:

WP DEBUG DISPLAY is a companion to WP DEBUG that determines whether or not debug messages are displayed within the HTML of pages.

The default value is ‘true,’ which displays errors and warnings as they occur. If you set this to false, all mistakes will be hidden. This should be used in conjunction with WP DEBUG LOG to allow for subsequent analysis of failures.

// Disable display of errors and warnings
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

Copy and paste this code following the screenshot to display WordPress errors.

Viewing the debug log:

Then, you can return to your site files and open https://yoursitename.com/wp-content/debug.log

SCRIPT_DEBUG:

SCRIPT_DEBUG is a related constant that will force WordPress to use the “dev” versions of core CSS and JavaScript files rather than the minified versions that are normally loaded. This is useful when you are testing modifications to any built-in .js or .css files. The default is false.

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );

Bonus Tips:

SAVE QUERIES:

The SAVEQUERIES definition saves the database queries to an array, and that array can be displayed to help analyze those queries. The constant defined as true causes each query to be saved, how long that query took to execute, and what function called it.

define( 'SAVEQUERIES', true );

PHP Errors, Warnings, and Notices

Enabling WP_DEBUG will cause all PHP errors, notices, and warnings to be displayed. This is likely to modify the default behavior of PHP, which only displays fatal errors or shows a white screen of death when errors are reached.

Showing all PHP notices and warnings often results in error messages for things that don’t seem broken, but do not follow proper data validation conventions inside PHP. These warnings are easy to fix once the relevant code has been identified, and the resulting code is almost always more bug-resistant and easier to maintain.

Custom PHP Debugging

If it is necessary to log non-error information for debugging purposes, PHP does offer the error_log function for this purpose. However, this method does not provide properly formatted output by default.

To address this, you may add another function on your site to handle formatting, either by creating a custom plugin or using a snippet with some code snippets plugin. The function will act as a wrapper for the error_log using print_r to format arrays and objects correctly before logging them.

Below is an example function that requires WP_DEBUG to be enabled.

function write_log( $data ) {
    if ( true === WP_DEBUG ) {
        if ( is_array( $data ) || is_object( $data ) ) {
            error_log( print_r( $data, true ) );
        } else {
            error_log( $data );
        }
    }
}

Usage Examples:

write_log( 'DEBUG TEXT' );
write_log( $variable );

Note: It is not recommended to add custom code like the above example in functions.php to avoid maintenance, security, performance, compatibility, and code organization issues.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

all-campaign
Golam Mostafa
Written by

Golam Mostafa

I'm a senior plugin developer for WordPress. I have four years of experience working as a software engineer for ADB Bank, and in 2018, I started developing plugins and founded ThemeDev.

Table of Content

Table of Contents

LTD Deals: 10+ Pro Plugins

Limited Time Offer
LTD Deals: 10+ Pro Plugins

Offer ends soon! Don't let it get away this time!

(NO RECURRING FEES)

Trusted by 25,099+ Customers

30 days Money Back Guarantee