Debugging in WordPress

Debugging in WordPress

David Nash code, wordpress Leave a Comment

I know, debugging in WordPress is boring! We’re creators – we want to build cool stuff without worrying about stepping on any toes! But debugging in WordPress is important!

Last week I came across this tweet from Jonathan Bossenger (@jon_bossenger):

My first instinct was to reply that enabling debugging in WordPress makes in impossible to use, until I looked more closely.

You see, every time I set up a WordPress site I have to edit wp-config.php, and that line define( 'WP_DEBUG', false ); occasionally tempts me to change it to TRUE. I soon regret it, because my next step is usually to open the WordPress admin dashboard. At which point I’m presented with something like this:

Debugging in WordPress
Debugging in WordPress doesn’t have to mean this!

So when debugging WordPress with the output set to the screen, I now need to scroll down to do anything. The page is just completely broken.

Back to the tweet above – the wisdom of it is telling us that we don’t have to output the debugging messages to the screen – they can be hidden and written to a file instead!

Now just going from the defaults in wp-config.php, I had no idea! Here are the final lines of that file:

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define( 'WP_DEBUG', false );

/* That's all, stop editing! Happy publishing. */

Okay, no I have to admit that it’s been a long time since I visited that link to the codex. But surely it would makes sense to include those other lines here, too – the ones that don’t entirely mess up your display and make the WordPress dashboard almost unusable!

Some saner defaults

I’ve created a ticket on the WordPress bug tracking system. I’ve never done that before, hopefully I’ve done it correctly and something comes of it.

Until then, this is what I think that section in wp-config.php should look like:

define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
define( 'SCRIPT_DEBUG', true );

With these set, WordPress will create a file called debug.log in wp-content/. Then from a terminal we can do something like tail -f wp-content/debug.log and keep an eye on that terminal to see if we’re introducing any new errors or warnings.

Having easy access to code warnings will make it easier to do things like creating your own autocomplete in WordPress (just a quick plug!).

If we all do this we can vastly improve our code, and therefore the experience of WordPress users (and other developers!) everywhere!

Leave a Reply

Your email address will not be published. Required fields are marked *