Debugging WordPress – How to Use WP_DEBUG On the Production Site

The WP_DEBUG is a PHP constant used in WordPress to debug the errors on the website. This constant displays all PHP errors, warnings, and notices in the HTML pages. It is recommended to keep WP_DEBUG ON during the development of a WordPress website. By keeping this constant ON, it will help developers to catch and resolve all types of errors. WordPress developers should make sure that all errors are handled before the site goes live.

However, there may be a situation where you have to fix errors in production only. There are some reasons for it. It can be because of newly installed or updated plugins, theme updates, changes in file permissions, etc.

When issues occur in production, usually it’s on high priority. You don’t have much time to move the production site to a staging server, fix the issue, and move back to production. A developer has to fix the issue on the production server itself. And again, the site should not break or display maintenance mode for live audiences(Here we are not talking about white death screens).

Though it seems tricky there are ways of fixing issues in production.

Restrict WP_DEBUG ON for Specific IP

If you have a static IP address, then to find out the error you can restrict WP_DEBUG ON for your IP. By doing this, the errors will display to only your IP. Live audiences will not see any errors. You can fix the issues and later remove the IP condition.

To get your IP address you can simply type What’s my IP in Google Search. Use the code below to enable WP_BEBUG for your IP.

wp-config.php

if ('YOUR_IP_ADDRESS' == $_SERVER['REMOTE_ADDR']) {
    define('WP_DEBUG', true);
} else {
    define('WP_DEBUG', false);   
}

Take note you must have a static IP address to perform this action. If your IP address is dynamic, then eventually the above code fails. WordPress will display errors to all visitors. Because each time PHP control would come in the else part.

Use Companions of WP_DEBUG

WordPress provides companions WP_DEBUG_LOG and WP_DEBUG_DISPLAY for WP_DEBUG. Using these companions effectively, you can find out the errors and possibly fix them. In this case, you don’t need to add any IP conditions.

wp-config.php

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

If WP_DEBUG_LOG is set to true it will create a debug.log file under wp-content/ directory. This error log file stores all types of PHP errors, warnings, notices, and stack traces. Using this log file, the developer would get an idea about the error and can fix it. You have to set WP_DEBUG_DISPLAY to false which will hide all errors on web pages. That means all errors will be logged in the debug.log file and your users don’t see any errors on a website.

This one is the recommended way of fixing issues with your WordPress website. After fixing the errors, don’t forget to set WP_DEBUG to false and remove the below 2 statements.

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

You may read more about debugging in WordPress in the official documentation.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “Debugging WordPress – How to Use WP_DEBUG On the Production Site

Leave a Reply

Your email address will not be published. Required fields are marked *