Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am pretty new to plugin authoring and was having a hard time debuging.

I used lots of echo and it is sloppy and ugly.

I am sure there is a better way to do this, maybe an IDE with a debugger that I can run the entire site in including the plugin?

share|improve this question
1  
I haven't seen an IDE that runs WordPress entirely within the IDE ... though that would be awesome. I debug plug-ins by running WAMP locally and code in Dreamweaver. If you set WP_DEBUG to false, as John recommends, then you get a pretty good idea of what's going wrong, if anything, within your scripts. Then you can edit in Dreamweaver, hit Ctrl+S and then F5 in the browser to immediately view the changes. –  EAMann Aug 12 '10 at 0:11
1  
@EAMann - Be sure to check out both PhpEd (for Windows) and PhpStorm+XDEBUG (for Mac, Linux and Windows). –  MikeSchinkel Aug 12 '10 at 17:06
add comment

13 Answers 13

Go into wp-config.php and change define('WP_DEBUG', false); to define('WP_DEBUG', true);. Also, install Andrew Nacin's Log Deprecated Notices plugin.

share|improve this answer
10  
I'd check out Nacin's other article too: andrewnacin.com/2010/04/23/5-ways-to-debug-wordpress –  kevtrout Aug 12 '10 at 15:10
    
With PHP 5.4+ you'll probably be inundated with E_STRICT notices. Drop this gist into the plugins folder and activate to remove Strict notices, deactivate to return to your normal service. –  webaware Nov 16 '13 at 23:20
add comment

If you're getting errors printed, then x-debug is a brilliant PHP extension that adds modern backtraces to PHP.

If you're trying to work out what's going on where there are no errors, my favourite approach is to define a function which logs its output to a file. So I do plog($variable) and that appears in the log file which I can then examine. This is especially useful when you're trying to work out what happened before header() is called, or other situations where you can't print to STDOUT.

share|improve this answer
add comment

Use xdebug + NetBeans IDE. When fully configured - which is easy to do - you can set break points in your plugin and watch variables at the break points. I think it is the best way to debug plugins or any php applications for that matter.

share|improve this answer
    
+1 for xdebug + NetBeans. Great for debugging plugins. –  webaware Jan 30 '13 at 1:00
add comment

After tinkering with a number of IDE's, I settled into plain old Notepad++ with an ultra-customized Syntax Highlighting color scheme.

I have a macro set up such that when I hit Shift-Ctrl-X , the following code gets output where my cursor is:

echo "<pre>";
var_dump($);
echo "</pre>";
exit();

It's simple, but I can generally hunt down 90% of my bugs with this macro plus WP_DEBUG enabled.

share|improve this answer
2  
Nice move. Keyboard hotkeys in editors rock! –  hakre Aug 25 '10 at 20:59
1  
I prefer the output given by print_r($var, true) instead of var_dump. –  brasofilo Jan 29 '13 at 21:48
add comment

I debug the old fashioned way, error_log()ing and var_dumping. I find that is the most efficient way for me, I have a couple of wrapper functions to handle different types of data, as error_loging arrays and objects can be a pain. Also, using print_r() in can be tricky to read when it is not in a <pre>. I have tj_log() for error logging, and tj() for showing output (which basically shows any data type in a presentable mannor:

function tj( $code ) {

    ?>
    <style>
        .tj_debug { word-wrap: break-word; white-space: pre; text-align: left; position: relative; background-color: rgba(0, 0, 0, 0.8); font-size: 11px; color: #a1a1a1; margin: 10px; padding: 10px; margin: 0 auto; width: 80%; overflow: auto; -moz-box-shadow:0 10px 40px rgba(0, 0, 0, 0.75); -webkit-box-shadow:0 10px 40px rgba(0, 0, 0, 0.75); -moz-border-radius: 5px; -webkit-border-radius: 5px; text-shadow: none; }
    </style>
    <br /><pre class="tj_debug">

    <?php
    if ( is_null( $code ) || is_string($code) || is_int( $code ) || is_bool($code) || is_float( $code ) ) :
        var_dump( $code );

    else :
        print_r( $code );

    endif;

    echo '</pre><br />';

}

function tj_log( $code ) {

    if ( is_null( $code ) || is_string($code) || is_int( $code ) || is_bool($code) || is_float( $code ) ) :
        $code = var_export( $code, true );

    else :
        $code = print_r( $code, true );

    endif;

    error_log( $code );

}

So then I just do: tj( $current_user ); or whatever.

share|improve this answer
add comment

I wrote a little class for make a logfile, is very useful when you are debugging ajax calls.

http://github.com/hunk/Magic-Fields/blob/master/tools/debug.php

You only needs do something like:

Debug::log("This is a debug message");

When that line is executed a the message will be added in the log file and after that you can use the tail command (if you are using some unix's style operative system)

tail -f mylogfile.log

If you can pass to this function a array or a object as well.

note you needs change the line 20 for a path where you want save your log file

share|improve this answer
add comment

I use Aptane IDE on Linux and UltraEdit on Windows, and this one also has a PHP-parser. Also, I view all hints from xDebug with the constant WP_DEBUG defined in wp-config.php.

See also my post on this topic and feel free to comment and give feedback about your development tools.

share|improve this answer
add comment

I recommend checking out FirePHP. You can send debug info to Firefox's Firebug over HTTP headers, which usually makes for cleaner debug output.

share|improve this answer
add comment

Not that bad too: Eclipse It's close to PhpStorm + free.

share|improve this answer
add comment

Krumo - the styled php debug class

Another really nice thing is the "krumo" php class. It's implemented in ½ min and offers an easy way to debug all sort of variables:

  • objects,
  • arrays,
  • strings/float/integer/etc.

Plus it helps with backtracing, shows loaded classes or included files and everything on demand.

Plus it's FREE!

Screenshot shows the javascript driven folding

Download

Krumo @sourceforge

share|improve this answer
add comment

There are two IDEs I can recommend and I've used both extensively: PhpED (Windows only) and PhpStorm+XDEBUG (Mac, Windows and Linux.) I'm on Mac now so can only use the latter.

Both of them ROCK! The good news that PhpStorm is $49 before September 2010 and only $99 after that. If I was on Windows and had to choose again, not sure which I would choose.

Frankly I can't help but feel that any plugin developer not using one of these two tools is severely handicapped, especially if they are relatively new to developing WordPress plugins.

share|improve this answer
add comment

I use a $13 plugin called LogPress you can buy on ThemeForest and it is an absolute God-send. You can debug everything relating to their plugins and site. Supports Firebug console logging and a whole lot more. I can't live without it, that is how much I use this plugin.

This plugin is probably the best money I have ever spent and it has saved countless hours in my Wordpress plugin development.

share|improve this answer
    
Wow, I was downvoted for recommending a paid plugin of which I have no association with whatsoever? That's a little heavy-handed aint' it? –  DigitalSea Feb 15 '12 at 2:53
add comment

I'm using phpED and xdebug but for me (and seems for somebody else) it's impossible to debug the plugins or theme's file! The debugger only stops on the breakpoints that are in the main or original "core" files! anybody can help me?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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