Debugging WordPress projects is one of those things that WordPress developers seem to approach differently.
Though there are certainly different ways to do it, I’m of the mind that using a debugger is the most effective way to, ahem, debug a project. I’m not saying that to try to be a purist, but the benefits of using an actual debugger far outweigh the alternatives.
But in WordPress, it seems that using facilities built into PHP are far more prevalent than using software specifically designed for debugging.
Sometimes, I think this comes with the nature of looking at other peoples code, and sometimes I think comes from not knowing where else to start.
But if there’s one thing that can not only help you become better at debugging projects and understanding source code, nothing can top debugging WordPress projects with an actual debugger.
Debugging WordPress Projects
One way to define debugging a computer program might simply be:
Finding a problem in a project and resolving it.
And sure, the definition can get more involved than that. But the point is that there’s a problem in the program, and it needs resolution.
Traditional WordPress Debugging
When it comes to debugging WordPress projects, far too often we see developers using things like
var_dump. These are all useful functions, but they aren’t ideal for debugging.
Sure, they are useful for seeing the value of an individual variable or a set of values at a given point in runtime, but when it comes time to tracing a problem from when it starts to where it likely lies, you need something a bit more powerful.
Secondly, there are also some useful and powerful plugins for helping us with the act of debugging WordPress. For example, Query Monitor is one of my favorite plugins.
Though it offers a ton of useful features and gives a lot of great information, I’d never recommend solely relying on it as a means to debug a given project.
Instead, that’s where debuggers come into play. So if you’re a WordPress developer, and you’re not using a debugger, I urge you to do so.
How Debuggers Are Different
Debuggers, as a piece of software, are different than using features of a programming language because they allow us to interact with the source code, at least in some capacity, while the program is running.
This means we can see what the program is doing at any given point while it’s running.
So if you’re not sure, say, why a value is being written to the database differently than you’d expect, you can follow the program all the way down into the WordPress core source code and then take a look a the value of what’s being written into a row.
This is, of course, but one example.
Furthermore, debuggers allow us to step into functions, step over functions, set points of execution where we want to stop and take a look at what the program is doing, and even – depending on the debugger – execute commands in a console to check out function output and variable values.
What About Log Files?
Naturally, log files are useful. But they are usually something we look to “after the fact” or after a program has executed.
They are great for seeing when something has thrown an error, notice, or a warning, or if something went wrong during execution but only after it happened.
I’ve previously written about the advantages of using a console to monitor log files when you are working with a project in development. But I think of them as an additional utility to help us get a complete picture of what’s going on with our work versus an optional way to debug our software.
Depending on the type of IDE you use will determine how you go about debugging WordPress. For example:
- PhpStorm has a built-in debugger that’s powerful.
- MacGDBp is my current debugger of choice.
- There’s a package for Atom that does a good job, but it’s UI leaves a bit to be desired.
- Codebug used to be something I recommend, but it seems to be defunct.
And there are other alternatives available so feel free to link them in the comments.
The point of this post is to help urge those of you who are working with WordPress and creating things with it are using proper tools for tracing bugs. Debugging WordPress is more about using language features of PHP and using external tools to
Debugging WordPress is more about using language features of PHP and using external tools to help interact with your code while working on a project.
It also gives you insight into WordPress code and can ultimately contribute to making you a better developer if it becomes part of your workflow.