Debugging is an essential skill for any developer. This tutorial will show you 11 powerful ways to debug WordPress and PHP errors.
The first item in the list is the famous WP_Debug, then we’ll jump into some more advanced methods.
First, let’s list the types of common PHP errors:
A – Notice: This is the least important error message you will see with PHP. It does not necessarily mean something is wrong, but there is possible improvement suggested.
Example: null element passed to a function that expect a string.
B – Warning: It’s a more severe error, but does not cause script termination.
Example: giving include() a file that does not exist.
C – Fatal error: This is a dangerous indicator of something going really wrong, and the script terminated.
Example: calling a non-existing function.
1 – WP_DEBUG
WordPress has a global constant to specify the needed level of debugging: WP_DEBUG. Along with this constant, comes two another important ones: WP_DEUBG_DISPLAY, and WP_DEBUG_LOG.
WP_DEBUG is used to set the debugging mode on or off. WP_DEUBG_DISPLAY shows errors or hides them. Finally, WP_DEBUG_LOG saves your error messages in wp-content/debug.log.
The three global constants can be set to true or false in wp-config.php, like this:
2 – is_wp_error()
Another WordPress built-in tool for debugging is is_wp_error();. It is a method to check if a certain result is of type WP_Error. WP_Error is actually the returned object you should receive if a WordPress method fails.
$post = array(
‘post_title’ => ‘Test post’,
‘post_content’ => ‘This is my post.’,
‘post_status’ => ‘publish’,
‘post_author’ => 1
$result = wp_insert_post( $my_post );
The above code will try adding a new post using wp_insert_post(). If this method fails, it will return WP_Error object. You can then catch and get the error message.
3 – Debug Bar
Another useful tool for debugging WordPress errors is the Debug Bar. It is a very handy tool for getting useful information about every page in your website.
After installing it, you will find a new Debug button. When you click on it, an analysis of the queries, templates, PHP installation, and many other useful information.
4 – Test Website
It is of the utmost importance to separate the live website from your tests and development. I usually have two installs of WordPress on my websites. This is important because you don’t want your scripts terminating when you turn on error reporting.
5 – Simply Show Hooks
Simply Show Hooks is a nice plugin for showing every hook that is running on any page. If you encounter a situation where all error reporting ideas are not working, and you will, then comes the time for fetching out every and all running hooks.
This plugin will tell you what action or filter hooks that run on any page. You can then start analyzing and debugging each hook code. You can also see the attached methods to every hook. And even find out the priority of each.
6 – WPDB Error Reporting
If you use the wpdb class for dealing with your database, then you will always need error reporting. Whether it is for making sure that your queries run correctly, or for showing up error messages for debugging.
// Before running your query:
$wpdb->show_errors = TRUE;
$result = $wpdb->get_results(“SELECT field_value FROM table_name”);
// Or you can choose to show the last tried query.
7 – Server Error Logs
At some points, neither WordPress or PHP will be able to catch some coding errors. For example, if your script exceeded the maximum allowed run time, you won’t get a PHP error message. Rather, Apache (or your server installed system) will pop up something like ‘Internal Server Error.’
This is the time when you should go to your error log, and see what if your PHP code, or a certain part of your WordPress installation did something wrong.
You can consult your web hosting provided on where the logging is stored. Usually it is something under logs folder.
8 – PHP Error Logging
PHP has its own level of error reporting to store the issues resolving beyond WordPress. This is extremely useful, especially if something outside of WordPress running and causing some problem.
You can start by configuring your php.ini file to turn on error reporting, and then choosing where to store such messages.
error_reporting = E_ALL | E_STRICT
error_log = /var/log/php_error.log
Setting the above two lines will turn on error reporting, and set the errors to be saved at the specified path.
Also, you can run phpinfo(); and check the error_log option.
9 – PHP Syntax Checker
If your hosting provider limits access to php.ini file, or you can’t access error logs, things can be a bit harder. But, there are many tools to overcome a situation when you get only a blank page with no error message. One quick tool is PHP Code Checker.
PHP Code Checker is a syntax errors checking tool. It is really useful if you missed a semi-colon or a curly brace, and can’t find where did you miss it.
10 – PHP IDE
If PHP Code Checker didn’t find a syntax error, then you will need a more powerful tool. A powerful IDE like PhpStorm will be the answer for more advanced debugging, and breaking down your code into parts.
In a situation like, when you store a string in a variable, and try printing it, but nothing happens. Using an IDE can find out you are doing something wrong like, overriding this variable later in your code. This is why it is highly recommended to rely on a powerful IDE like PhpStorm, Eclipse, or any tool you prefer.
11 – Disabling Browser Cache
To disable Google Chrome browser cache, open the DevTools by right-clicking of any web element. Then, click on Network tab in toolbar. Finally, check the Disable Cache checkbox at the top.
The above 11 tips can be your guide for the debugging process. Although debugging can be tedious at many times, but the above tricks can make the whole process much easier.
By Loai Nagati