Debugging WordPress Tips and Snippets

THE BASICS

Like I said, WP_DEBUG is the common debug flag we use when working with WordPress. You normally see something like this in the wp-config file define( 'WP_DEBUG', 'true' ); This is what’s called a constant; it’s accessible anywhere from within WordPress itself. I, personally, use a more robust debug log, like so:

define( 'WP_DEBUG', true );
if ( WP_DEBUG ) {
define ('JETPACK_DEV_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'SCRIPT_DEBUG', true );
    if ( defined( 'DOING_AJAX' ) && DOING_AJAX || defined( 'DOING_CRON' ) && DOING_CRON ) {
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
} else {
define( 'WP_DEBUG_DISPLAY', true );
}
}

I know, it’s a lot to take in! So let’s break it down, shall we?

Jetpack Debug

One notable constant you’ll see in this snippet is define( 'JETPACK_DEV_DEBUG', true ); This constant sets Jetpack into debug mode in your development environment. If your project doesn’t use Jetpack, you may not need this one, but Jetpack is a daily thing so I always have it.

Logging

The next is WP_DEBUG_LOG, this is an absolute MUST if you are debugging in WordPress. What this does is provide a debug.log file in your /wp-content/ directory for you to read. What I like to do, since I have more than one monitor, is to open a terminal and dedicate one monitor to the debug file. To do this, if you’re not familiar, you can tail the file like so: tail -f wp-content/debug.log this will stream the file to your terminal. Every time a new error shows up, you’ll see it.

If you’re new to the command line, and have a Mac, a simple process you can follow is:

  • Type tail -f in your terminal
  • Navigate to the wp-content directory of the project you’re working on. If there is no debug.log file there, simply create it.
  • Once the debug.log file exists, drag it to your terminal window to right after the tail -f you typed. This will output the full file path location in terminal
  • Hit enter. Once you do that, you’ll see when items get added to the log on the fly.
  • To stop the tail output, hit ctrl + c on your keyboard
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.