Yesterday I had a bug to fix that once I did, realized I should give PHPStorm its credits. Here’s the scenario:
- I wrote a plugin that runs a query every hour to get GravityForms leads, convert to CSV file and post to remote FTP server;
- There was no error message or any indication that there was a problem in the code (see pic 1), even with WP_DEBUG set to true, when manually running the scheduled task.
- The only information I had was that the query did have records that were not outputting to the CSV file.
Debug WordPress Cron Tasks
So, how to debug if there’s no error message? Well, here are the steps:
- Make sure to disable all the scheduled tasks. On your wp-config.php file add:
//This will allow to run the task you want to debug individually. define('DISABLE_WP_CRON',true);
- I use this great plugin to see the WP Cron events, it allows you create your own schedules and run them individually (as in the pic 1), named WP CONTROL
- Then, using WP Control, edit the event you want to debug and set to a time in the past (pic 2).
- Now, with WP_DEBUG set to true, go to this URL: http://yoursite/wp-cron.php?doing_wp_cron
You then can see the error message. Like the one below:
- Now, how to find the problem in the code? Well, the orange line in the pic 3 above shows exactly where the problem is.
Solving the Problem With PHPStorm
PHPStorm is a poweful IDE for writing PHP code. I was introduced to it early this year at WordCamp Orange County’s presentation from Joe Chellman on debug WordPress for real. (I tried to find the video but seem not available). Here’s his presentation slides: http://www.shooflydesign.org/files/wcoc-2015/#/
So, after I setup PHPStorm to debug my web application (I plan on posting about this sometime soon), set some breakpoints in my code, here are some screenshots that make me think PHPStorm is worth every penny (It costs under $100/personal license).
PHPStorm also allows you get information right in the code panel. See example below:
And there’s more 🙂 You can setup dynamic breakpoints so they will only fire if the condition is met:
This new method to debug have saved me tons of time fixing issues like this one. But not only that, it allows you also step line by line on others peoples code and see what’s going on under the hood. If you develop in PHP, I strong recommend taking a look at PHPStorm.
Do you use other IDE? Are you already using PHPStorm? Did you have a hard time setting up WordPress Xdebug on your development enviromment? Let me know your comments and experiences.