Easy WordPress Debugging Tips
In case you didn’t know, debugging in WordPress is essential. Not only does it save users from plugin conflicts and enormous headaches but it is a valuable learning tool to help you code more cleanly and efficiently.
The following are some easy tips to get you started:
print_r()Put any variable inside one of these two functions and see what it’s made of.
print_r()will even return a value. If you ever wonder what your code is doing (or why it doesn’t work) this is a good place to start.
WP_DEBUG()is a constant in your config.php file. Set it to
'true'in your development environment and
'false'in production. This will display both warnings and fatal errors. Warning are often due to uninitialised variables and conditions that don’t have alternative
elsestatements. A must if you are developing a theme or plugin!
WP_DEBUG_DISPLAYSo let’s say you’re debugging on a staging or a production server. You want to log any errors but you don’t don’t them to be visible as that would be ugly and also make your site more vulnerable. These guys are your friends. All you need to do is have this little snippet in your config.php file and your errors will be logged to a file named
debug.logfile that will be generated inside the
wp-contentdirectory. Please note that you actually have to have errors for this to work.
Some of this may seem obvious for tried and true php developers but for anyone new to WordPress theme or plugin development these can make the difference between code that throws errors (and uses memory to do so) and efficient application of code. WordPress debugging is also invaluable when trying to modify or use copied and pasted code.