Debug WordPress Like a PRO

If you build or maintain WordPress websites, sooner or later you will run into issues. There are many ways to go about debugging in WordPress, but there it is how the professionals do it.

Activate debug log

Turning on debug mode and displaying the errors on frontend is one ugly business. It’s neither easy to use nor aesthetically pleasing. And it’s a big no-no if your site is live. So, what the professional does is logs the errors, especially if you don’t have access to the main web server logs.

To enable the debug mode logs in WordPress, you add the following lines to your wp-config.php.

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

View the logs in real time

The WordPress debug log location is wp-content/debug.log. It saves all errors, warnings and notices in there. Now you can see the issues in real time as they happen by using this tail Linux command:

tail -f wp-content/debug.log

Custom logs

Debug logging is very handy when you build your own stuff. When you have things you need to dig deep into actions and filters,especially AJAX ones, there is no direct way to display them in the screen, so you log them like a PRO.

error_log( $var_to_debug );

It will log the whatever you pass to the function to same debug.log file. As a results, you won’t lose a single hair out of debugging despair.

error_log() only logs strings, so if you want to debug an array or object, you’ll have to run it through print_r() first and then return it to error_log(). Like this:

error_log( print_r( $var_to_debug, true ) );

Stop debugging like an amateur and start doing it like a PRO!