One Weird Trick to Debug WordPress Quickly

Have you ever needed to build a new feature or fix some bugs for a WordPress site? Chances are the code you need to work with is way too complicated to understand quickly.

wtf-code

You can use a combination of  var_dump()  and  wp_die()  to do some basic introspection, but that’s really just a snapshot of a moment in the code.

What you need to understand to fix the bug is to know where the application execution started, what was triggered and how you ended up at a certain point. What you need is a trace.

Here’s a function I just wrote for getting a nice and comprehensible call trace. It is probably more resource-intensive than some other alternatives but it is short, understandable, and gives nice output (Exception->getTraceAsString()).

Example output:
1) /var/www/test/test.php(15): SomeClass->__construct()
2) /var/www/test/SomeClass.class.php(36): SomeClass->callSomething()

This function is so useful!

You get a really nice list of what led to a function being executed, complete with file locations and the exact line of code there the next function is called.

Instead of wasting spending time in Sublime Text jumping between files doing the same thing manually, use generateCallTrace() and learn in minutes what might have taken you days to fully grasp.

Get New Posts via Email

I don't always post, but when I do, you can get it sent directly to your inbox.

Join 10 other subscribers

  • Adim Ofunne

    Where do you put this function in wordpress? How can it be executed in a real project, for us that are just getting into the wordpress eco system

    • I put it one line above where I needed to use it, then used it!

      No need to find the right home for it since it’s a temporary addition 🙂