Wordpress - 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!


WordPress comes with it's own debugging system that is using constants, making it easy to turn on/off when you are switching environments. All constants are defined in the wp-config.php file.

define( 'WP_DEBUG', true )

Setting WP_DEBUG to true will cause all PHP errors, warnings and notices to be displayed on the screen. Also, it will throw notices about deprecated functions and arguments.

define( 'WP_DEBUG_LOG', true )

Enabling WP_DEBUG_LOG will save all errors in a file( debug.log located in /wp-content/). It is useful when debugging AJAX events.

define( 'WP_DEBUG_DISPLAY', true )

As an addition to the previous two constants, WP_DEBUG_DISPLAY can be used to control whether or not we want to print the errors on the page.

define( 'SAVEQUERIES', true )

When SAVEQUERIES is defined as true, every query that is executed will be saved. Also there are information about how long a query took to execute and what function called it. All information is stored in $wpdb->queries.

Also there is a SCRIPT_DEBUG constant define( 'SCRIPT_DEBUG', true) which forces the WordPress to use the development versions of the core CSS and JS files, instead of the minified versions.


Using Visual Studio (you will need PHP Tools)/PHPStorm/Eclipse, you can do debugging with Xdebug. If you are using XAMPP/MAMP, it already has Xdebug installed, so you will just need to configure it.

Locate php.ini and add these two lines:

zend_extension="/absolute/path/to/xdebug.so"
xdebug.remote_enable = 1

There are other options that can be configured abd you can read about them in the xdebug documentation. Once you finish with editing the php.ini file, restart your Apache server.

The next step is to set the PHP Debugger settings in your editor. Navigate to the Settings/Preferences dialog and locate the PHP Settings. Once you are done with that, you can start setting breakpoints.

Tags:

Debug