6

In Visual Studio, you can easily debug your application by running Debug mode and setting breakpoints in your application.

In Wordpress, how would you achieve same thing?

I have an issue in my website and there are no error or warnings thrown. I have spent enough time tweaking settings to figure this out without luck. The only solution I've found is to set breakpoints and debug the application like in .NET.

I know that if you enable Debug mode in Wordpress, it prints out a gigantic debug output on top of the site - this is fine, but I just cannot debug efficiently with that.

Please let me know if there is any better approach for debugging WP sites in Visual Studio.

1
  • Why is debugging wordpress different then debugging any other php software on a web server? If you will find a php development enviroment that lets you have breakpoints it will work for wordpress as well. Commented Jun 2, 2016 at 3:16

3 Answers 3

9

Debugging in wordpress can be a bit difficult when your first starting out, while I don't use breakpoints such as what Visual Studio offers, I can offer advice on how to debug php in general, as well as turning on the debugging output from wordpress.

First start by opening up your wp-config.php and finding the following line.

define('WP_DEBUG', false);

By default this is set to false, switch out the second parameter to true. This will enable the php notices, warning, and errors messages to be displayed in your browser.

A quick view of the Wordpress Codex shows you can enable database related error messaging with a quick:

<?php $wpdb->show_errors(); ?> 
<?php $wpdb->print_error(); ?> 

Doing this will allow you to view the last sql query sent out, and any error it may have encounted back from MySql.

Then in general when trying to figure out what's wonky about variables or data structures I find this is always helpful:

<pre>
 <?php print_r($arrayOrObject);
</pre>

Using the pre tags will spit out raw output without any html preformatting coming in, scrunching your view of associative arrays and objects. var_dump() functions are also useful in this way.

When trying to debug something like a postback, ajax request and you want to get into the middle of it simply echo out the variables you are trying to debug and immediately issue a die() command, ex:

echo '<pre>';
echo var_dump($arrayOrObject);
echo '</pre>';
die();

For development enviorments I prefer using something along the lines of a WAMP stack (MAMP for mac osx off the top of my head.)

It's definitely useful to run a local copy of your wordpress instance on your own machine, allowing you to experiment with the above techniques as required. The above mentioned troubleshooting tips have gotten me through the last 5 years of php programming and wordpress programming. Hope you find this useful!

8
  • Excellent first answer. Welcome to WPSE Nebri! Please feel free to take the tour.
    – Tim Malone
    Commented Jun 2, 2016 at 0:58
  • Thanks for your answer. I'm not really a beginner but I just cannot see what's the best way to debug your app in Wordpress. Normally Wordpress just worked without any testing anything. I'm in a tricky situation because site is growing larger and new issues are arising. I want to incooperate debugging and unit testings on my theme and custom plugins but don't know where to start with this.
    – Jason
    Commented Jun 2, 2016 at 4:27
  • And I will try out your approach and come back with result about it, talk to you soon.
    – Jason
    Commented Jun 2, 2016 at 4:28
  • I'd also be interested in exploring unit testing in wordpress. I'll see what I come up with over the weekend and update my answer if I find anything useful :).
    – Nebri
    Commented Jun 2, 2016 at 14:50
  • @EnglishMaster how did you make out my friend?
    – Nebri
    Commented Jun 7, 2016 at 13:20
6

WordPress comes with it's own debugging system that is using constants, making it easy to turn on/off when you are switching environments. All constants are defined in the wp-config.php file.

define( 'WP_DEBUG', true )

Setting WP_DEBUG to true will cause all PHP errors, warnings and notices to be displayed on the screen. Also, it will throw notices about deprecated functions and arguments.

define( 'WP_DEBUG_LOG', true )

Enabling WP_DEBUG_LOG will save all errors in a file( debug.log located in /wp-content/). It is useful when debugging AJAX events.

define( 'WP_DEBUG_DISPLAY', true )

As an addition to the previous two constants, WP_DEBUG_DISPLAY can be used to control whether or not we want to print the errors on the page.

define( 'SAVEQUERIES', true )

When SAVEQUERIES is defined as true, every query that is executed will be saved. Also there are information about how long a query took to execute and what function called it. All information is stored in $wpdb->queries.

Also there is a SCRIPT_DEBUG constant define( 'SCRIPT_DEBUG', true) which forces the WordPress to use the development versions of the core CSS and JS files, instead of the minified versions.


Using Visual Studio (you will need PHP Tools)/PHPStorm/Eclipse, you can do debugging with Xdebug. If you are using XAMPP/MAMP, it already has Xdebug installed, so you will just need to configure it.

Locate php.ini and add these two lines:

zend_extension="/absolute/path/to/xdebug.so"
xdebug.remote_enable = 1

There are other options that can be configured abd you can read about them in the xdebug documentation. Once you finish with editing the php.ini file, restart your Apache server.

The next step is to set the PHP Debugger settings in your editor. Navigate to the Settings/Preferences dialog and locate the PHP Settings. Once you are done with that, you can start setting breakpoints.

2
  • Thank you Boris, will definitely be exploring the additional options you've mentioned in my free time :).
    – Nebri
    Commented Jun 2, 2016 at 12:18
  • Hey @Boris, thanks. I definitely need Vagrant set up to do xdebug thingy. My site is hosted on WPEngine, can't change their php.ini. I'm working on Vagrant set up atm. I wish I could select 2 answers though.
    – Jason
    Commented Jun 8, 2016 at 2:46
0

I just succeed step debugging wordpress using Eclipse IDE for PHP Developers,
and WAMP server for running wordpress locally.
I mainly follow below guide: https://brainyloop.com/debugging-wordpress-with-eclipse-php-development-toolkit/
the tricky part is that I have to enable the step Debugger following https://xdebug.org/docs/remote
Hope this help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.