Debugging is an important phase on the Internet world that involves detecting the errors and dangers in a computer program and finding out the possible solutions for reducing the errors. The development environments now come with unique tools and plugins to perform the debugging process. WordPress has specific debug system that eases the job of debugging the PHP code and helps in improving the overall quality of the supported sites, which in return increases the efficiency and productivity of this web software.
In the following, we’d like to come out a comprehensive beginner’s guide about how to debug WordPress with the help of WP_DEBUG.
In order to debug WordPress, there is a specific PHP constant named as WP_DEBUG, which is used in the program code to turn on the debugging system at the time of execution of the program code. This WP_DEBUG constant is a Boolean type variable that is assumed to be false by default and is set to true at the time of program code execution. A general syntax is given below
It is essential that every software user needs to follow the correct syntax to avoid any logical errors. This syntax can be used at places where the programmer wishes to turn on the debugging system. But, it is highly recommended in places where the user uses the code to add plugins and themes in the website.
WP_DEBUG serves to be one of the debugging tools among many others to debug WordPress, resulting in displaying all kinds of errors and warning notifications that often produces too much of error and messages jamming. It shows even errors that do not seem to break the program code execution. To be frank, it works like the spam filters to some extent, but has more powerful functionalities.
This option cannot be used on the live website execution; instead it is used in the program code. It is set to false at a stage when the user thinks that the debugging process is done which might either be at the end or in the middle of the program code. The user can turn it on and off whenever he wishes to and wherever he wants to in the program code.
There are some other extensions to the WP_DEBUG tool that are discussed as below.
It is often recommended to have the error files logged in a separate file that helps for reviewing it in the future. WordPress provides the users with the facility of error logging that occurs during the debugging process. These files are stored in a debug.log file and are saved under the/wp-content/ directory.
This is an automatic process but if the user wants to review the log file later in the program, he can make use of the following code.
Another extension to WP_DEBUG is the WP_DEBUG_DISPLAY. Since, the debugging tool displays all the errors and warning messages that might not be harmful enough to break the code execution, some users wish to hide the error messages from being displayed in the HTML pages of the websites. This could be done with the following syntax.
The other constant of the debugging system in WordPress is SCRIPT_DEBUG, defining the script version that might be useful in testing modifications. Some users make use of the minimal script versions that are normally supported in most of the development environments, but some prefer using “dev” versions that have no restrictions for any environments which is set on with the following syntax.
Some users prefer saving the queries submitted in the website in an array location, which helps in analyzing the queries better. The following syntax is used for enabling this function
One fine example that describes all the functions put together looks something is like as below.