How to debug php code while developing wordpress plugins?

16,027

Solution 1

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.

Solution 2

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

Solution 3

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:

Solution 4

Other things that may be useful to you:

1) Plugins that look for deprecated functions in your code, such as Log Deprecated Calls or Log Deprecated Notices.

2) Setting the WP_DEBUG constant will provide useful information in the PHP log.

Share:
16,027
sun
Author by

sun

Learning Web Technology with the help of SO:) I Love movies.

Updated on June 23, 2022

Comments

  • sun
    sun about 2 years

    I started developing few WordPress plugins on my own. While developing a plugin i am using different hooks(wp_head, add_shortcode, etc) function in the plugin. Can anyone advice me an easy and convenient way to debug a WordPress plugin or is there any other way to develop a WordPress plugin easily. Thanks in advance.