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.

Related posts

Leave a Reply

5 comments

  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.

  2. The Best way is to Download the Plugin Query Monitor

    Then you can go ahead and add the line

    do_action('qm/debug', <your_data>);
    

    And this will Log everything in the log section.
    To open Query Monitor just go to the admin panel and click on top admin bar you’ll find some metrics like speed, just click on that.