Wiping the mystery out of WordPress debugging
Sterling Camden
Speaking of the deficiencies of PHP, another one is the complete lack of built-in debugging capabilities. Sure, you can find debuggers for PHP, if you’ve got the time and ability to configure them on your server. I guess if I was really serious about PHP I’d have to get one of those, but so far I only use PHP for WordPress customization, so it hardly seems worth the effort.
Some people will tell you that you shouldn’t need a debugger. That it’s a sign of lazy programming. But PHP has so many pitfalls, and WordPress has so many actions and interactions between plugins and themes provided by so many different developers, that I think you’re excused if you need a little help figuring out just what the heck is going on in your code.
When writing a plugin or modifying a theme for WordPress, usually all I need to know is the order of events and the values in various variables. Sometimes you can get away with inserting “echo” statements in your code — but sometimes you can’t, like when your code executes ahead of a “wp_head” action that wants to add headers. Besides, randomly placed “echo” statements can easily get lost from view in the whirl of words and images that comprise your theme and content.
So, I developed a simple little plugin that collects debug statements and prints them out nicely above the footer of your theme, or below the admin footer, whichever gets executed first. I call it WiPeD, for “WordPress Printf Debugger”. Anywhere in your code, you can call WPD_print() and pass it whatever text you want to output. The lines of text are aggregated in a session variable until they are output together (in separate divs, surrounded by a single containing div). Thus you can use styling to set the messages off from the rest of your blog, if you desire.
Here’s an example, from a project I’m currently working on. My plugin was producing no results, and I couldn’t figure out why. So I added some calls to WPD_print():
Then, when I executed the code to trigger this action, here’s what I saw at the bottom of my page:
That tells me that I got into my function and passed the tests, but the post has no content at that point. Now I just need to figure out why.
See the enclosed readme file for more details. Have fun debugging your PHP!
UPDATE: get an updated version here.
Posted in PHP, Web, Wildly popular, WordPress |
19 Comments » RSS 2.0 | Sphere it!





Excellent! If I find myself hacking much more WordPress code, you can bet money I’ll be using this (unless something better comes along by then). Of course, at present I’m looking more for alternatives to hacking WordPress code so I won’t have to — but the problem of migrating content and ensuring I don’t lose some of the most important functionality kinda has me tied to WordPress for now.
Anyway . . . it’s great seeing you produce a WordPress hacker’s debugger. It’s something WordPress desperately needs.
Thanks apotheon, I needed it for myself. Yes, warts and all, I won’t be leaving WordPress any time soon — there’s just too much that it does for you that I’d have to replicate.
Hi, unfortunately the download does not work. It returns a 22 bytes long file.
Regards,
Seb
Oops! How the heck did that happen? Sorry about that — it’s corrected now. Thanks for the heads up.
Ran on localhost apache server:
Error message during do_action in plugin.php – it doesn’t like the callback you added to the array in add_action
Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, ‘WiPeD::setup’ was given in … \wordpress\wp-includes\plugin.php on line 123
Now I need a debugger to debug the mysteries of the debugger!
What version of PHP?
Php5 (5.2.1.1)
What version of WordPress? 2.2.3? I’m on 2.3.
I upgraded from 2.1.3. to 2.3.1 but the debugger still gives the same error. However the problem I was trying to debug (google sitemap) was fixed by the wordpress upgrade.
That’s very strange. I assume it also fails using the latest version of the plugin?
I wonder if there’s some setting in PHP that might affect this.
[...] I was coding along one fine day, happily using my WiPeD debugger for WordPress, when I suddenly stumbled on a problem with it. I was attempting to debug a function that was [...]
[...] Wiping the mystery out of WordPress debugging – Sterling Camden provides a solutions for debugging your WordPress plugins. That’s such a cool name, isn’t it? “Hello. My name is Sterling… Sterling Camden… 007.” [...]
Hi, si created a debug plugin for wordpress making use of Pear::php_debug for php5. Feel free to check it out here:http://www.communitymodder.com/Released-wordpress-plugins/wp-pear-debug-wordpress-plugin.html
Cool, thanks!
hi, andar here, i just read your post. i like very much. agree to you, sir.
Thanks, andar.
[...] worse because for some reason it cleared all the roles from the current user. Then I found the WiPeD debug plugin which was simple to [...]
Thanks for your simple but powerful debugging plugin.
Glad you like it, Frank. Do you have any suggestions for enhancing it?