Start Debugging WordPress Plugins Today!

The very first thing you should do as a WordPress plugin developer is make sure your developing site has debug mode turned on.  This may seem like an obvious task but some developers forget to enable it.

When I first starting creating simple WordPress plugins, I actually didn’t have debug mode turned on.  This made it very difficult for me to determine if what I was doing was right or wrong.  After I turned debug mode on, it was like opening my eyes for the first time.  I saw all of my plugin’s errors as well as a bunch of errors from other plugins.  This made me think, how many developers out there actually have debug mode enabled and debug their code?  Granted, most of the errors from other plugins, were “PHP Notice” errors, which is basically saying that the code will run but there is still something wrong.  Most of the time these errors can be fixed very easily.  For example, if you have code like this:

if ($array["key"] == "value") {
    // do something
}

and $array does not have an index called “key”, then you will get an “Undefined Index” notice.  This can be fixed by making sure $array[“key”] is defined before trying to compare it to “value”.

if (isset($array["key"]) && $array["key"] == "value") {
     // do something
}

This code now checks to make sure $array[“key”] is defined before trying to compare it to “value”.   So, this is a very simple example of how debug mode can help you recognize errors you otherwise would not have seen.  Follow the steps below to enable debug mode.

  1. Go to your top directory and find wp-config.php, and open it.Wordpress_install_files
  2. Scroll down until you see
    define('WP_DEBUG', false);
  3. Replace it with these lines of code
    // Enable WP_DEBUG mode
    define('WP_DEBUG', true);
    
    // Enable Debug logging to the /wp-content/debug.log file
    define('WP_DEBUG_LOG', true);
    
    // Disable display of errors and warnings 
    define('WP_DEBUG_DISPLAY', false);
    @ini_set('display_errors',0);
    
    // Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
    define('SCRIPT_DEBUG', true);

Here is the official WordPress debugging page.  This is a very quick and simple task so please make sure you always have debug mode on.

Please leave any comments below and email us at feedback@wpdevadvice.com or support@wpdevadvice.com with any further questions.

Leave a Reply

Your email address will not be published. Required fields are marked *