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 echo, print_r, and 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.

Query Monitor

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.

Debugging WordPress with MacGDBp

Debugging WordPress with MacGDBp

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.

OS X Console

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.

Debugging 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.

Category:
Articles
Tags:

Join the conversation! 9 Comments

  1. I use NetBeans and Desktop server to debug. They might not be the best, don’t know, but they are free, which as a hobbyist WordPress developer fits my needs.

    This somewhat out of date article helps to get it all set-up.

    https://serverpress.com/using-netbeans-for-wordpress-debugging-development/

    The only annoying thing I’ve found is that Xdebug will hang, and not give any error, if your security software is blocking the port it uses.

    steve

  2. PHPStorm was a game changer to me. The inline debug tools, conditional breakpoints, xdebug seemless integration make it worth every penny. On top of that, if you are active in an open source project the license is free.

    • PhpStorm is a great IDE – it doesn’t fit my style, personally, but I’ve nothing against it. A lot of what it has built-in is fantastic and are tools that I recommend for any developer if they opt to use the IDE.

      If not, then you can still get generally the same functionality but it takes several apps to do it.

  3. Hi Tom,

    Great article!

    I never used Query Monitor plugin. Will give it a shot. Currently, I am using this package https://atom.io/packages/php-debug

    Another package is this https://atom.io/packages/wp-dev

    Regards.

  4. Would be nice to see a tutorial on how you use your debugger of choice like MacGDBp

    • I’ll see if I can write something up in the coming weeks. It’s not a bad debugger, but it takes a little more work to get integrated with a separate editor than Codebug did, so if I find something similar then I may use it, too.

      Regardless, I’ll see about writing something up about debugging and WordPress.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.