Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug php code while developing wordpress plugins?

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.

like image 656
sun Avatar asked May 12 '13 10:05

sun


4 Answers

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.

like image 162
webaware Avatar answered Nov 15 '22 17:11

webaware


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:

  • http://wordpress.org/extend/plugins/wp-debug-logger/
  • http://wordpress.org/extend/plugins/wp-log-in-browser/
like image 32
m4t1t0 Avatar answered Nov 15 '22 18:11

m4t1t0


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

like image 2
Lance Cleveland Avatar answered Nov 15 '22 16:11

Lance Cleveland


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.

like image 1
Stephen R Avatar answered Nov 15 '22 17:11

Stephen R