Everyone make mistakes. I know that I do. We also look for the ways to correct our mistakes. In programming, this process is called debugging. According to Wikipedia,
Debugging is the process of finding and removing errors which results in the smooth execution of a computer program.
Debugging plays a very vital role in the process of software development. It provides ways to identify and resolve different errors that occur during and after the development of a software.
In this article, I am going to talk about debugging in WordPress. How can you debug your WordPress website and different plugins which are available for logging errors.
Debugging in WordPress
When I started development in WordPress, I did not know how to debug my code for errors. The debugging mode is turned off by default. As a newbie, there was no way for me to know that if I was doing something wrong or my code is generating any errors because there were no errors on my screen.
Eventually, when I did not get the required results from my code, I knew that something was wrong in my code. So I started my quest for the process of debugging in WordPress. To my surprise, I found out the there is a whole section of Debugging in WordPress on the official codex of WordPress.
WordPress provides some PHP constants which we developers can use during the development. I am going to explain the use of these constants one by one and how you can use them to debug your website in WordPress.
Using WP_DEBUG Constant
WP_DEBUG is a PHP constant provided by WordPress for the purpose of debugging. This variable lives inside wp-config.php present in the root directory of your WordPress installation. It is set to FALSE by default because WordPress do not want you and your viewers to see ugly errors lying around on your website. You can change the value of this constant to TRUE during development so that you can view the errors on your website.
Look for the following line of code inside wp-config.php:
Now change the value of this constant to TRUE, just like shown in the following code.
Hurray! or should I say oops 😉 You just enabled the debugging mode of WordPress. Now, you will be able to see each and every error that occurs on your website. You should use this method very carefully. Developers mostly use this mode during the phase of development. After this phase is over, they change the value of this constant back to FALSE.
There are various reasons due to which this method should never be used on a live website. One of the important reason is that using this way, PHP errors and notices can appear on the pages of your site. It can show locations of your files present on your site or other sensitive information which should not be displayed.
In short, a website should not show any errors on any page because it can be a threat to its security.
Disable display of PHP notices
Since no errors or warnings should be allowed to show on the pages of a website, you can disable their accidental display using the following code. Open the file wp-config.php and copy and paste the following code at the end of this file.
// Disable display of errors and warnings. define( 'WP_DEBUG_DISPLAY', FALSE ); @ini_set( 'display_errors', 0 );
WP_DEBUG_DISPLAY is another PHP constant which WordPress uses to control the display of errors in the HTML of the pages of your website. The default value of this constant is TRUE. Setting this constant to false will hide all the notices. It is also important to note that if you want this constant to work, then the value of WP_DEBUG must be set to true.
Using WP_DEBUG_LOG Constant
It is another very useful constant which comes out of the box with WordPress. This constant allows you to log every error that occurs on your website to a file named debug.log present inside /wp-content/ directory. First of all, add the following line of code immediately after WP_DEBUG inside wp-config.php.
define( 'WP_DEBUG_LOG', TRUE );
After adding the code, go to /wp-content/ and create a file with the name “debug.log”. You can view this file anytime to see the errors which have occurred on your website. And debug them according to the message displayed in this log file.
Some WordPress Error Logging Plugins
WP Log Viewer enables you to view the current status of your debug log via your admin bar. It only takes one simple click to view, download and sort different types of errors and notices. Its auto refresh feature keeps the log file updated with the latest errors. There are many other features available for you to explore.
Log Viewer allows you to view your log file from with admin dashboard. It refreshes the view of the log file every 15 seconds to allows the latest errors to be view right away. It also lets you perform operations like dumping and emptying your log file.
Debug Bar provides a link in the admin bar to view the errors and notices occurring on your website if you have enabled the debugging mood of WordPress. It also provides details about your site’s memory usage and object cache.
In this post, I have shown you, how you can debug your website in WordPress, which PHP constants WordPress provides for debugging and how to log and view the errors and notices that your site produces from the dashboard of your site with the help of different plugins.
So which plugin do you use to view your error log? Do you know about any other error logging plugin which I should have mentioned in my post? Share your feedback in the comment box below or reach out to me via Twitter @AsharIrfan.