As a theme or plugin developer it is highly recommended that these developers must debug their code while working on it. Since they plan to release their products publicly, Debugging PHP code is part of any project. Thankfully WordPress comes with specific debug systems designed to simplify the debugging process as well as standardize code across the core, plugins and themes. This article describes the various debugging tools in WordPress and how to be more productive in your coding as well as increasing the overall quality and interoperativity of your code.
In-built WordPress Debug Tools :
1. WP_DEBUG :
It is a PHP constant (a permanent global variable) that can be used to trigger the “debug” mode throughout WordPress. It is assumed to be false by default and is usually set to true in the wp-config.php file on development copies of WordPress.
define('WP_DEBUG', true); define('WP_DEBUG', false);
Note: The true and false values in the example are boolean (true/false) values.
It is not recommended to use WP_DEBUG or the other debug tools on live sites; they are meant for local testing and staging installs.
Enabling WP_DEBUG will cause all PHP errors, notices and warnings to be displayed. This is likely to modify the default behavior of PHP which only displays fatal errors and/or shows a white screen of death when errors are reached.
Enabling WP_DEBUG will also cause notices about deprecated functions and arguments within WordPress that are being used on your site. Deprecation notices often indicate the new function that should be used instead.
2. WP_DEBUG_LOG :
It is a companion to WP_DEBUG that causes all errors to also be saved to a debug.log log file inside the /wp-content/ directory. This is useful if you want to review all notices later or need to view notices generated off-screen (e.g. during an AJAX request or wp-cron run).
3. WP_DEBUG_DISPLAY :
It is another companion to WP_DEBUG that controls whether debug messages are shown inside the HTML of pages or not. The default is ‘true’ which shows errors and warnings as they are generated. Setting this to false will hide all errors. This should be used in conjunction with WP_DEBUG_LOG so that errors can be reviewed later.
4. SCRIPT_DEBUG :
5. SAVEQUERIES :
The SAVEQUERIES definition saves the database queries to an array and that array can be displayed to help analyze those queries. The constant defined as true causes each query to be saved, how long that query took to execute, and what function called it.
The array is stored in the global $wpdb->queries.
NOTE: This will have a performance impact on your site, so make sure to turn this off when you aren’t debugging.
WordPress Debugging Plugins :
1. Debug Bar :
It adds a debug menu to the admin bar that shows query, cache, and other helpful debugging information.
2. Debug Bar Console :
It adds a PHP/MySQL console to the debug bar. Requires the debug bar plugin.
This plugin logs the usage of deprecated files, functions, and function arguments. It identifies where the deprecated functionality is being used and offers the alternative if available. This plugin also logs incorrect function usage, which WordPress started reporting in 3.1.
4. Total Security :
The Total Security plugin is the must-have tool when it comes security of your WordPress installation. The plugin monitors your website for security weaknesses that hackers might exploit and tells you how to easily fix them.
5. Query Monitor :
A WordPress plugin for monitoring database queries, hooks, conditionals, HTTP requests, query vars, environment, redirects including automatic AJAX debugging and more.
I hope you find this article helpful. If you have any query or suggestions to achieve the same result as above plugins does, do share with us.