Simple Debugging with WordPress
For simple WordPress theme development, what is the first thing most PHP developers do to troubleshoot problems?
print_r( $post ); die();
One the statements are in place, the programmer refreshes the page and looks at the source to view a nicely indented array or object. Next they comment out the print_r and die statements, change some lines, and try the code again. If it fails, they are back to square one and in go the print_r,echo and die statements so the brutal cycle can begin again.
We know these methods partially work (who hasn’t done the above during a PHP project!), but we also know they are less than optimal. Is there a better way?
WordPress Debug Mode
WordPress offers quite a few ways to enable and customize a debug mode while developing. To enable debug mode, you want to define WP_DEBUG as true in your wp-config.php. Here is the complete code block I suggest you use. We will look at the individual parts shortly. Place this in your wp-config.php file after the lines that define the database variables. Be sure to change development_user to be your development database user name:
@ini_set('display_errors',0);
if( 'development_user' === DB_USER ){
define('WP_DEBUG', true); // Turn debugging ON
define('WP_DEBUG_DISPLAY', false); // Turn forced display OFF
define('WP_DEBUG_LOG', true); // Turn logging to wp-content/debug.log ON
}
display_errors
The first line in our code block turns off the display of errors, regardless of php.ini or .htaccess settings to the contrary. This is important because though WordPress can force the display of errors to be on, it won’t force them to be off if display_errors is already turned on.
Testing for local vs. production
There are probably fifty ways of doing this part, so use a method that works for you. In my environments, my local development database rarely if ever has the same user name as the production database. Simply checking what I expect the local user name to be against what is defined in DB_USER is a simple way of knowing if the files are on the development or production servers.
WP_DEBUG
This is the most important constant as it determines if WordPress will use any of the other debugging constants. Thankfully it is quite simple. If set to true, debug mode is turned on. If undefined or set to false, debug mode is kept off.
WP_DEBUG_DISPLAY
This constant confused me at first, but it is actually quite simple. If set to true, it will override the current display_errors configuration setting and turn it on. However, if set to false it will not turn off display_errors. This setting is simply an override. This is the reason we turn display_errors off before setting any of these constants.
Set this constant to true if you want to see errors displayed in your browser when they occur. Keep in mind this will sometimes complicate errors because once at least one error has been written to the page, all redirect requests will fail. This is because the header information can no longer be modified once content is sent, and a displayed error counts as content.
WP_DEBUG_LOG
This constant is the whole reason I found the WordPress debug methods so helpful. Set this constant to true and WordPress will set up PHP to write to an error log in wp-content/debug.log. Sadly you can’t specify a different file location that is not in the content folder, but at least you know where it is!
Note: because this is written to a public directly, be extremely careful not to upload the log by mistake. Additionally, if you run WP_DEBUG on a production server, do so only for immediate troubleshooting then turn it off and remove wp-content/debug.log.
Leveraging WP_DEBUG
In addition to PHP errors being sent to debug.log we can also send our own output using the error_log PHP function. The only problem with this, is even in production mode your errors will still most likely be written to a PHP log; it just won’t be debug.log. Because of this, its a good idea to provide a wrapper function to handle the logging for your theme or plugin.
Place the following code in your functions.php in your theme, or in the plugin file for your WordPress plugin:
if(!function_exists('_log')){
function _log( $message ) {
if( WP_DEBUG === true ){
if( is_array( $message ) || is_object( $message ) ){
error_log( print_r( $message, true ) );
} else {
error_log( $message );
}
}
}
}
Feel free to expand on this function if it doesn’t exactly meet your needs, but the concept is simple: Centralize all your log calls to use your custom function. Then, in that _log function, only output the message if WP_DEBUG is set to true. There is no reason to test if WP_DEBUG has been defined because as soon as wp-settings.php is processed, WP_DEBUG will be defined, even if it wasn’t already defined in wp-config.php.
This particular _log snippet will call a print_r on arrays and objects passed to the function for simple debugging.
Trying it Out
If you have followed these steps, you can run a quick test by adding these lines into your header.php file in your theme folder:
_log('Testing the error message logging');
_log(array('it' => 'works'));
After refreshing your page once, you should see a newly created debug.php file with a few lines of output. Use any log viewing utility that supports tailing the file for maximum productivity. On Mac, Unix and Linux systems, you can use this command from the main directory of your site:
tail -f wp-content/debug.log
More Solutions
If you are looking for a more interactive console for debugging, be sure to look at our review on the WordPress Console.
What about you? If you didn’t already know about this built in WordPress feature, what methods did you come up with to make debugging WordPress easier? Please leave us your tips in the comments below!
Doug Neiner is an Editor at Fuel Your Coding and an official member of the jQuery Team. He is addicted to new technology, and specifically loves spending time with WordPress, Ruby on Rails and jQuery. Learn more via twitter or his Google Profile.


Yeah, but why would you ever use Wordpress when you have Chyrp around?
because Chyrp is dead. http://chyrp.net/2010/05/14/chyrp-is-dead-long-live-chyrp/
Great post, thanks very much :)
Great post. Just what I was looking for. Just wish PHP/WordPress had more info right in the browser. A blank page isn’t that helpful.
Note: In your paragraph that reads “After refreshing your page once, you should see a newly created debug.php file…”, it should be “debug.log”, right?
Thanks again for the debugging tips!
Thanks for the great tips. Would love to see some debugging tips for backend work on custom fields, custom types or plugins here as well.
I followed your instructions and I did not see any debug.log file show up.
@Ken You need to create the debug.log file in the wp-content directory and then “chmod 666 debug.log”. Please refer to http://lists.automattic.com/pipermail/wp-hackers/2010-September/034830.html.
You can specify a different log file, and by extension, location by setting `ini_set` in your functions.php file.
You might have some code a bit like this:
`
if ( WP_DEBUG_LOG && WP_DEBUG ) {
ini_set( ‘error_log’, WP_CONTENT_DIR . ‘/logs/’.date(’Ymd’).’.log’ );
}
`
As long as you’ve created the log directory, and set the permissions appropriately your debug data should be safe from prying eyes.
gr8 post man.
i think in the case of a plugin its best to simply
send those lines into the header so when you
turn off the plugin after debugin you won’t have
to remove those..
A small function plus an action should do the trick..
would have pasted it but you know… php and comments :)
Anyhow.. thanks
Well, this is awesome. Just wondering if there is way to check live error records on windows development machine running xampp and wordpress. I am looking something like “tail -f /var/www/html/site/error_log” kind of thing. Is it possible?
Check out an excellent ‘tail’ utility for Windows: Hoo Win Tail (http://www.hootech.com/WinTail/). It’s a $49.95 utility that is well worth its price for anyone who frequently peruses log files (it makes looking through log files a lot more pleasant than ‘tail -f some.log’). Since my workstation runs Windows and development server runs Linux, I’ve set up Samba shares so that I can use Hoo Win Tail to keep tabs on the debug files that live in the Linux file system.
Here’s a clickable link that works: http://www.hootech.com/WinTail/
Overall, a great idea but print_r() is not the best choice. The array pointer is set to the end, thus providing very confusing output.
Either replace with var_export or follow print_r with reset();
if(!function_exists(’_log’)){
function _log( $message ) {
if( WP_DEBUG === true ){
if( is_array( $message ) || is_object( $message ) ){
error_log( var_export( $message, true ) );
} else {
error_log( $message );
}
}
}
}
Also neither can be used with ob_start buffers.
See http://ca.php.net/manual/en/function.print-r.php
Thank you. This is perhaps the best written set of instructions for debugging WordPress that I’ve stumbled across.
Do you know how we can get the gallery images to go to a URL we type in? I can’t get the native gallery to jump on my site (http://agreatdream.com/word-list-vegetables/) I want the verb list to go to a page not the image. I don’t want to use NextGEN Gallery as it doesn’t work with a lot of other plugins I need.
Glen
You are probably will be interested in FirePHP Debugger plugin for wordpress debugging.
Followed most of your tips. In the end the root index.php file turned out to be blank for some reason so it wasn’t loading the theme. I copied a working root index.php from another WP installation and voila my site was back up. Posting this so it may help others.