UDPATE : 6 Jan 2011 – Added the Debug Bar plugin to the list of plugins.
If you develop plugins or themes for WordPress, then you surely have to enable some sort of debugging mechanism. If not then you are flying blind and the possibility that your code has bugs increases tenfold. There are many ways to debug in WordPress and this post is intended to roundup all the options in one page, so you can pick and choose which one(s) work for you.
Probably one of the most famous WordPress constants. Define it in your wp-config.php file and “debug mode” will be enabled. When enabled, errors and warnings are displayed.
WordPress Codex : ‘Editing wp-config.php’ - Debug section
A bit of info from the codex about WP_DEBUG, SCRIPT_DEBUG.
5 Ways to Debug WordPress
A must-read from Andrew Nacin. The post goes over 5 ways to debug WordPress, including:
- The ‘all’ and ‘shutdown’ hooks
- Core Control plugin
Enable Debugging And Logging For Live Site Usage
This starts with the same sort of idea as Joost’s above article and expands on it.
Actions and Filters
From a code point of view, WordPress contains programming hooks that allow you to either attach functionality (actions) or modify data (filters). So obviously, one way to extend WordPress is to create your own actions or filters. Here are some articles on WordPress action and filter hooks:
Debug WordPress hooks – by Andrey Savchenko
WordPress: Oh Those Actions and Filters! – by kovshenin
WordPress Hooks Database – by Admin Brown. Adam has put together a list of EVERY hook within WordPress and shows when the hook was added and if it is deprecated. Amazing!
Plugin API/Action Reference – This WordPress.org Codex article lists all action hooks available for use in plugin development.
White Screen of Death
If you develop for WordPress then I bet you have seen the infamous White Screen of Death (WSoD) at some stage before. James Huff sums up the WSoD perfectly in his post : “Nothing can be more unsettling than an obvious problem with absolutely no error or diagnostic message, and that’s exactly what the infamous white screen of death is“.
In most cases, a quick solution is simply a matter deactivating all your plugins. While this might seem extreme and you will probably loose most of your site’s functionality, at least the site will work and visitors can browse it!
Here are some articles on the subject and how to resolve it:
WordPress and the White Screen of Death by James Huff
Beware the WordPress white screen of death by Joab Jackson
How to troubleshoot WordPress “White Screen” of Death by Jared Heinrichs
Debug Bar – Requires WP 3.1 and hooks into the new admin bar. It shows you many different bits of info about queries, cache, warnings and much more!
Log Depricated Notices – Logs the usage of deprecated files, functions, and function arguments, and identifies where the deprecated functionality is being used.
Core Control – Core Control is a set of plugin modules which can be used to control certain aspects of the WordPress control. Currently, Core Control features modules for managing Filesystem Access, Managing plugin/theme/core updates, Managing HTTP Transports & External HTTP Request logging
Debug Objects – provides a wide variety of info to the user to help with debugging, including memory usage, constants, file permissions, queries, theme info, cache content and much more!
Dump Environment – Prints the classes and variables for debugging purposes.
Debug Queries – list all DB queries to find the slow performing ones.
Step Into Your Code!
I find the best way to debug my code is simply to “step into” the running code to see exactly what is going on and what the different variable values are. I have written an article a while back on doing exactly this, using NetBeans PHP IDE and XDebug. This has saved me countless hours in development time. Go check out how to set it up:
Yoast’s Debug Theme
Yoast has created a special WordPress theme especially for debugging. Now this is nice and useful in a development environment, however, I would never suggest enabling the theme on a live site. In his article, Yoast talks about using the theme together with the Theme Tester plugin so as admin, you can get the benefits of the debug theme without your users having to see it. Brilliant!!
So there are a number of ways to debug your WordPress code. Some, in my opinion, are under-utilized while being very powerful. With a little up-front effort, you could really improve your productivity and code output. If you are having bugs and issues, use some of the techniques or ideas mentioned above to find and squash the bugs.
This doesn’t mean you should do it only on your code, do it for other code too. Too many WordPress user are so quick to tell you that your plugin or theme is broken and crap, but they never investigated why it broke or where. Most times the plugin developers are too busy at the precise moment you need their help. And it’s not their fault, but you can do something about it yourself now. You could debug the problem and fix it yourself! Heaven forbid!!!
And remember, before you release that plugin to the world please test, test test!!!