Debugging in WordPress

Debugging in wordpress can be a bit difficult when your first starting out, while I don’t use breakpoints such as what Visual Studio offers, I can offer advice on how to debug php in general, as well as turning on the debugging output from wordpress.

First start by opening up your wp-config.php and finding the following line.

define('WP_DEBUG', false);

By default this is set to false, switch out the second parameter to true. This will enable the php notices, warning, and errors messages to be displayed in your browser.

A quick view of the WordPress Codex shows you can enable database related error messaging with a quick:

<?php $wpdb->show_errors(); ?> 
<?php $wpdb->print_error(); ?> 

Doing this will allow you to view the last sql query sent out, and any error it may have encounted back from MySql.

Then in general when trying to figure out what’s wonky about variables or data structures I find this is always helpful:

<pre>
 <?php print_r($arrayOrObject);
</pre>

Using the pre tags will spit out raw output without any html preformatting coming in, scrunching your view of associative arrays and objects. var_dump() functions are also useful in this way.

When trying to debug something like a postback, ajax request and you want to get into the middle of it simply echo out the variables you are trying to debug and immediately issue a die() command, ex:

echo '<pre>';
echo var_dump($arrayOrObject);
echo '</pre>';
die();

For development enviorments I prefer using something along the lines of a WAMP stack (MAMP for mac osx off the top of my head.)

It’s definitely useful to run a local copy of your wordpress instance on your own machine, allowing you to experiment with the above techniques as required. The above mentioned troubleshooting tips have gotten me through the last 5 years of php programming and wordpress programming. Hope you find this useful!

Leave a Comment