How to debug php code while developing wordpress plugins? How to debug php code while developing wordpress plugins? wordpress wordpress

How to debug php code while developing wordpress plugins?


Using a PHP debugger can be good, but it can also be a bit like "follow the bouncing ball". For simplicity, enable WP_DEBUG and WP_DEBUG_LOG (see Debugging in WordPress) and use the error_log() function to dump useful information to the wp-content/debug.log file.

I tend to prefix log statements with the class method, function, or include file name, so that I know where they've come from. e.g.

error_log(__METHOD__ . ": value = $value");error_log(__FUNCTION__ . "\n" . print_r($_POST, 1));ob_start();var_dump($collection);error_log(basename(__FILE__) . "\n" . ob_get_clean());

The Debug Bar plugin can also be pretty handy, especially with some of the available add-ons.


For debugging I usually use the standard php function to inspect variables, you know, var_export and print_r. If I have a bug that is more difficult to detect, then I use Xdebug: http://xdebug.org/.

In addition, in Wordpress you can use this plugins to log the content of your variables:


The Debug Bar plugin is a great start when combined with turning debug mode and debug logging on in the wp-config.php file.

Debug Bar

Debugging In WordPress, debug and debuglog settings