Debugging In WordPress
For the experienced developers, encountering errors while working on a project is a normal thing to happen. But for a casual tweaker or a new learner, this can give a headache. Imagine, after writing multiple lines of scripts and it does not work. Yet you have no idea where is the source of the problem.
Type Of PHP Errors
Before we go further, it is good to know more about PHP errors in WordPress. Generally, there are four categories of PHP errors namely Parse Error, Notice Error, Warning Error and Fatal Error. Below is the example of PHP errors which typically occurs in WordPress.
Parse errors occur when there is syntax mistake in the scripts. It also happens when you forgot to include a character in the scripts. A parse error stops the script execution. There are many reasons for the occurrence of parse errors. Here are few of it possible cause:
- Missing ; at the end of a line.
- Using curly quotation instead of straight quotation marks.
- Missing a $ when referencing a variable.
- Using ELSE statement without opening IF statement.
This is most common yet least important error. The existence of this error doesn’t mean there is something wrong with the script. It also will not stop PHP execution. However, there is a possibility of improvement suggested. For example, notice error will be created when an undefined variable or undefined index is called during script execution.
As its name says, warning error is more severe than notice error. Similarly, it also won't stop script execution. Among the reasons which will cause warning error is:
- Using include() on a non-existing/missing file.
- Using the incorrect number of parameters in a function.
A fatal error is an indicator implying that something is wrong with your website. This error will terminate your script immediately. Below are few reasons which will cause a fatal error:
- Using require() or require_once() on a non-existing/missing file.
- Calling a non-existing function.
Debugging In WordPress
For non-techies, you might be wondering about what is debugging. Debugging is the process of identifying and removing errors from computers software and hardware. It is highly recommended to use debug mode while working on any project.
By enabling debug mode, you can ensure that your codes are 100% truly errors free. It is because some of the errors may not visible on your website. Below is the example of an error in WordPress.
By default, debug mode it is disabled in WordPress. However, you can turn it on by making a small change in your wp-config.php. Before you make any changes, don't forget to backup your file. Also, it is not recommended to use debug mode on live site as they were meant for the testing site.
To enable it, open up your wp-config.php and locate a PHP constant called define('WP_DEBUG', false); and change its value to define('WP_DEBUG', true);. By doing this, you have enabled a debug mode for your WordPress site. But I would strongly recommend you to use debug mode together with its companion. Please read further below.
WP_DEBUG_DISPLAY is a companion for WP_DEBUG. This constant is used to control whether error messages are shown in the HTML page or not. It is set to true by default which will show all errors as they are generated. Below is the usage of WP_DEBUG_DISPLAY in wp-config.php.
WP_DEBUG_LOG is another companion for WP_DEBUG. As you might guess from its name, this constant is used to save all errors into a debug.log. You can locate this log in wp-content directory. This is practically useful if you want to review all of the errors at the later time.
Let say, you want to hide all errors from being displayed in your HTML page. But instead, saving it into a debug.log for later review. You can do this by adding these constants in your wp-config.
Aside from that, there is also another handy constant you can use for debugging in WordPress. This constant is called SAVEQUERIES. It is used to save database queries into an array for analyzation purpose. By using this constant, you can save all queries including the time it took to execute the query as well as what function is called.
You can find the array which is stored in the global $wpdb->queries. Although this constant is very useful, please do not forget to turn it off when you aren't debugging anymore. Because it will give an impact on your site performance.
Aside from method stated above, you can also use a plugin for debugging in WordPress. There are few good debugging plugins available in WordPress. Please note that you can only use these plugins for debugging in WordPress development site.
Debug Bar is a plugin from WordPress.org which adds a debug menu to the admin bar. This plugin shows query, cache, and other useful information. It will make errors tracking are easier, however, you still need to add debugging constants in your wp-config.php.
Unlike its name, Query Monitor actually is a plugin that can be used for various type of debugging. It has many advanced features that aren’t available on other debugging plugins. Aside from monitoring database queries, hooks, and HTTP requests; it can also use for debugging AJAX calls, REST API requests, redirects, and much more.
Log Deprecated Notices
Log Deprecated Notices, on the other hand, is a plugin that identifies whether the deprecated functionality is currently being used. This plugin will log the usage of deprecated files, functions, and function arguments thus will offer an alternative if available. WP_DEBUG isn't needed but for this plugin to works, but its usage is still strongly recommended.
Obviously, debugging is an important tool either for a developer or a new learner. But, many of us forgot to use it or didn't know about the existence of debugging tool. I hope this article will give an idea about debugging in WordPress especially for those who are new to WordPress. If you have any debugging tips or other debugging plugins, please kindly share it with us in the comment section.
He is a web developer, a programmer, and a computer technician. He obsessed with coding and enjoy learning new things. In his spare time, he likes to play online games, musical instruments or watching anime and movies.