“Friends don’t let friends develop without WP_DEBUG.” —Andrew Nacin
Nacin gave a fantastic talk about Debugging in WordPress at WordCamp San Francisco 2011. I’ve heard more than one developer highly recommend this talk as a way to get started with debugging.
Debug Constants for Your Testing Environment
define( 'WP_DEBUG', true ); // Show all notices and errors
define( 'WP_DEBUG_DISPLAY', null ); // errors on, errors off, let php.ini decide
define( 'WP_DEBUG_LOG', true ); // Log errors to wp-content/error.log
define( 'SCRIPT_DEBUG', true ); // De-minify admin CSS and Javascript
define( 'SAVEQUERIES', true ); // DO NOT run this in production
Plugins
Common PHP Functions
- error_log, var_export, print_r
- var_dump and sometimes die
- debug_backtrace
Tracking Things Down
- Step 1 – Disable plugins and switch to the default theme.
- Check cron, rewrites, roles & capabilities.
- Go to Settings ? Permalink to flush rewrites.
- Are query vars set properly?
- Does the SQL look right?
- Rewrite rule matched?
- Funny redirect? Check canonical redirects with
remove_action( 'template_redirect', 'redirect_canonical' ); - Testing multisite? Setup multisite and switch to multisite any time with
define( 'MULTISITE', true );to wp-config.php. - Dig into the codebase with phpxref, grep, ack.
- Learn the stack.
- Learn what things call what.
Common Traps
- Make sure your code is actually running with
die( 'wtf' );orerror_log(); - White screen on the frontend with no errors, go to Appearance ? Themes to reset the theme.
- Internal server errors? Check home and siteurl, then .htaccess, then canonical.
- Moved or missing widgets? Make sure the sidebars have IDs.
- Slow site with lots of pages? Check permalink structure (pre-3.3).
- Make sure there are no extra “
Template:” calls in style.css. - Strange admin behavior. Check table structure, step through upgrades by major release.
- bbPress rewrites fail after activation. Re-register your post types before you
flush_rewrite_rules();
Local Development
- Map a domain to 127.0.0.1 using /etc/hosts
- Run a staging server by replacing links with an output buffer by adding to wp-config.php
ob_start( 'switch_to_dev_urls' );
function switch_to_dev_urls( $buffer ) {
$live = 'http://EXAMPLE.COM';
$dev = 'http://DEV.EXAMPLE.COM';
return str_replace( $live, $dev, $buffer );
}
More
- Xdebug for backtraces and profiling
- KCacheGrind visualization
- Use an IDE
- Unit testing