Themergency fooplugins.com
wordpress debugging netbeans2

Debugging WordPress Like A Master

| 8 Comments

How do you debug issues in your WordPress themes or plugins? Up until recently I have been doing it the wrong way : inserting echo and print_r statements into the source. There are obviously a few issues with this. The main one being that you have the chance of releasing code to the world with crappy debugging variable dumps (this has happened quite a few times in the past with plugins that I have released). So I decided to make a change.

Visual Studio Quality Debugging

I come from a Microsoft Visual Studio background, and I must say I was a bit spoilt when it comes to debugging. VS comes standard with the ability to step into code and look (and change) variable values in realtime. I love the fact that I can set a breakpoint, run the project and go directly where I want to go with minimal effort. My goal was to find the same debugging awesomeness with WordPress. And I have succeeded!

  1. The first step was to find a capable IDE. And the easy answer is NetBeans PHP IDE. The current version out is 6.9. Download and install it. You won’t be sorry when you switch from Notepad++, or whatever other smart text editor you are currently using. NetBeans has a powerful PHP source code editor, built-in intelli-sense, PHP unit-testing, MySQL integration and much more.
  2. You then need to install and configure XDebug on your development machine. XDebug allows you to debug your PHP scripts properly. Goto the Tailored Installation Instruction page to test your local PHP configuration to see which binary to download and install. You will need to paste the HTML output from phpinfo() and paste it into the textbox provided. Submit it and it will give you a download link and instructions to follow.
    • Aswell as adding a path to the XDebug binary included in their istructions, you also need to add the following lines to your php.ini file:
      xdebug.remote_enable=On
      xdebug.remote_host="localhost"
      xdebug.remote_port=9000
      xdebug.remote_handler="dbgp"
    • I came across a slight issue when following the provided XDebug instructions. I was asked to add the line zend_extension = C:\Program Files (x86)\PHP\ext\php_xdebug-2.1.0-5.2-vc6-nts.dll to my php.ini file. It did’nt actually work until I surrounded the path with quotation marks e.g. zend_extension = C:\Program Files (x86)\PHP\ext\php_xdebug-2.1.0-5.2-vc6-nts.dll
  3. Restart your local web server.
  4. Look at your phpinfo() output again to ensure you now have XDebug enabled. You should see something similar to this:
  5. Fire up NetBeans. Create a new project and point it to your WordPress folder.
  6. Open a PHP script file that you know will be included i.e. your plugin that is activated in the WordPress backend. Add a breakpoint on a line by clicking the line number in the margin.
  7. Click the “Debug Project” button or Ctrl+F5. If all went well, it should open the site in a browser and immediately break into WordPress’s index.php file.
  8. Hit F5 to continue and it should break on your breakpoint you set earlier.

Mission Accomplished

Now you can:

  • step through the code one line at a time to see where and how your code logic is working
  • check variable values
  • check the Call Stack to see how you got to your current code position

That is just what I was after, so mission accomplished. Now there are no excuses to not test your code before releasing it, and there is absolutely no excuse to include echo or print_r statements in your code again!

2 comments
  Livefyre
  • Get Livefyre
  • FAQ
StijnDeWitt
StijnDeWitt

To prevent the debugger from breaking at the first line of the index.php file (which can become quitte annoying after a while):


Tools -> Options -> tab Debugging, uncheck 'Stop at First Line'.

JohnJonesJr

Very nice approach, traditionally ive http://wordpress.org/extend/plugins/debugwp/

Steveorevo
Steveorevo

Excellent. Two free resources: DesktopServer (XAMPP that includes the debugger & WordPress) + NetBeans for PHP. Together, you can get rolling on Mac or Windows in a few clicks (not to mention a many more bonus features):

 

http://serverpress.com/news/using-netbeans-for-wordpress-debugging-development/

Jeff
Jeff

Thanks for posting this. I'm not a pro, so how the debugging worked took me a few minutes, but it helped. I debugged a new function that I added and really got a view of how it worked.

Trackbacks

  1. GoTop’s Blog » 调试WordPress says:

    [...] 于是在网上找了半天,试了各种方法,后来看到一篇文章Debugging WordPress Like A Master, 提到XDebug也可以用来debug WordPress. [...]

  2. [...] the running code to see exactly what is going on and what the different variable values are. I have written an article a while back on doing exactly this, using NetBeans PHP IDE and XDebug. This has saved me countless [...]