So you’re developing with WordPress. Whether you’re working on your own plugins, themes or doing client work, you’ll eventually run into the need of debugging something. Most people will simply print the data they need to debug to the screen but I’m not a big fan of this. When working on WordPress, or any web application, I don’t want to search through the browser where the data/error is being printed. I want a separate screen where all my data is logged in a clear way.
Turn WP_DEBUG on
Let’s start with stating the obvious. Set WP_DEBUG
to true in your wp-config.php
. Your notices, warning and errors will be suppressed when you leave WP_DEBUG
set to false. If you’re developing anything in WordPress, you must work with WP_DEBUG set to true.
Enable Error logging
Instead of simply printing data to your screen, for example with var_dump
, I like to log my data to the error log. By default WordPress will log errors in /wp-content/debug.log
. Before WordPress will log errors, you must enable it. Below code snippet includes all the WP and PHP settings you need for WordPress to log and display errors.
Using the Error Log
Now that you’ve enabled error logging you can log any data you want to the debug.log
file by using the error_log
function. Below example with send a test string to the log file.
The first parameter of error_log
, the message, is a string. Want to log an object or array? No problem. Simply use print_r
.
Note the second argument of print_r
. This makes sure the output of the function is returned instead of being printed. I advise you to use the following command in your terminal to keep a ‘live stream’ of your debug.log
file: tail -f debug.log
. Note that this command has to be run from the directory where the debug file is. This command will work on Mac and Unix.
So how do you debug in your WordPress projects? Did you find this article helpful or did I miss something? Be sure to let me know by leaving a comment below.
I like to use Codelobster to debug WordPress code.
I’ve just stated tinkering with WP code. Thanks for the time and tips, I’m sure I’ll find it useful in the near future.