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 Error

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:

  1. Missing   ;   at the end of a line.
  2. Using curly quotation instead of straight quotation marks.
  3. Missing a   $    when referencing a variable.
  4. Using   ELSE   statement without opening   IF   statement.

Notice Error

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.

Warning Error

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:

  1. Using include() on a non-existing/missing file.
  2. Using the incorrect number of parameters in a function.

Fatal Error

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:

  1. Using require() or require_once() on a non-existing/missing file.
  2. 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.

Debugging In WordPress
Fatal error example 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

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

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.

SCRIPT_DEBUG

By default, WordPress loads a minified version of your Javascript and CSS core files. This is excellent for a live site which will give you faster load times by decreasing its file size. However, for development purpose, you may want to turn this off. There is SCRIPT_DEBUG constant which will load the non-minified version of your Javascript and CSS.

SAVEQUERIES

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.

Debugging Plugins

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

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.

Query Monitor

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.

Conclusion

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.

  • If you think your friends would find this useful, please share it with them. It will allow us help more people.
  • To get more of our latest update, please subcribe to our newsletter.
  • You can also print this article for reference.
This article has been printed from https://www.jebatonline.com/articles/debugging-in-wordpress/

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.