43

I've recently inherited a WordPress plugin that has a few bugs in it. My problem is that I'm also new to WordPress and I don't know how to log debug messages so that I can figure out what's going on.

I really just need a way to create a popup or log to a console.

1
  • 1
    just figured out a simple hack to display javascript alert messages. Add: echo "<script> alert('Hello World!'); </script>"; This should get me going but I'd really like to know the community standard so that other people won't be lost in my code. And, I hate putting messages like this - that have to be deleted - in my code. Much rather use a system where debug messages can be turned off and on in one place! Commented Jan 26, 2013 at 22:16

6 Answers 6

33

There's this excellent Q&A at WordPress Stack Exchange, lots of knowledgeable folks explaining their debugging techniques: How do you debug plugins?

In the Javascript arena you basically need <script>console.log('the value is' + variable);</script>. And use Google Chrome inspector and/or Firebug.

In PHP, it depends on where things are happening or where you want the output.


Debugging in WordPress

Official documentation in the Codex.

Example wp-config.php for Debugging

// 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 );

Printing information to a log file

The following uses an OSX/Unix/Linux system path, adjust for Windows.

/* Log to File
 * Description: Log into system php error log, usefull for Ajax and stuff that FirePHP doesn't catch
 */
function my_log_file( $msg, $name = '' )
{
    // Print the name of the calling function if $name is left empty
    $trace=debug_backtrace();
    $name = ( '' == $name ) ? $trace[1]['function'] : $name;

    $error_dir = '/Applications/MAMP/logs/php_error.log';
    $msg = print_r( $msg, true );
    $log = $name . "  |  " . $msg . "\n";
    error_log( $log, 3, $error_dir );
}

Then, in you code call the function my_log_file( $post, 'The post contents are:' );


Print directly in the rendered Html

/* Echo variable
 * Description: Uses <pre> and print_r to display a variable in formated fashion
 */
function echo_log( $what )
{
    echo '<pre>'.print_r( $what, true ).'</pre>';
}

And wherever needed use it like: echo_log( $post );.


FirePHP

This extension will log information directly in the browser console. Refer to the following Q&A at WordPress Answers: How to use WP-FirePHP extension?.


Query Monitor

This is a must have on a debug toolkit, the plugin has many features, one of them is its Logs tab, just drop this in your code and have it listed in the plugin interface in your page:

do_action( 'qm/debug', 'This happened!' );
do_action( 'qm/debug', $your_var );
do_action( 'qm/debug', [$var1, $var2] );

Sign up to request clarification or add additional context in comments.

Comments

5
  1. Don't develop without debugging!
  2. Read this please: http://wp.smashingmagazine.com/2011/03/08/ten-things-every-wordpress-plugin-developer-should-know/

Good luck and you may keep us updated.

Comments

3

The general PHP debugging strategy is to use print_r( $var ) statements and refresh the page. Simple and easy. If you want to step into the code Xdebug is what you want to install.

Comments

1

According to your comment where you Much rather use a system where debug messages can be turned off and on in one place:

It can be done in WordPress. There is a constant called WP_DEBUG that you can set to true or false from your wp-config.php file in your WordPress folder (that file where you add database parameters ).

So, you can use:

if( WP_DEBUG ){
echo "<script> alert('Hello World!'); </script>"; 
}

The alert will show only when you have WP_DEBUG set to true (for example on a development version of the website like on your localhost) while it won't show on your production version of the website(You just have to set WP_DEBUG to false there).

2 Comments

In most cases Wordpress simply silently fails with plugins displaying nothing even with debugging enabled.
You should check your PHP settings and server logs.
0

I'm aware that my answer comes several years after the original question but, since I recently had the same exact problem and still couldn't find a satisfying solution, I coded a plugin for that.

It specifically addresses the OP need:

I really just need a way to create a popup or log to a console.

Hope it helps all the plugin/theme developers out there, who are looking for a quick and easy way to debug their code https://wordpress.org/plugins/bugfu-console-debugger

Just install it and call the logging method from your PHP code to log straight to the browser JavaScript console.

BugFu::log($your_string-array-object);

EDIT August 2022

Technique used when you log something calling the method above:

  1. PHP (Backend) writes to a database log
  2. JavaScript (Frontend) makes an AJAX call to read the database log
  3. PHP (Backend) reads the database log and returns its content
  4. JavaScript (Frontend) prints the returned content to the Browser Console

Installing BugFu you create a bridge between PHP and JavaScript, which makes it quick and easy to log to the Browser console, from wherever you are in your WordPress code (theme or plugin), even from PHP code which is only run as an AJAX callback.

No need to worry about turning on and off WordPress debug logs, no need to access the hosting server to read the usual PHP logs, no need to install special browser extensions or IDE tools.

7 Comments

Seems great! But it would be awesome if you could include here the concept that translates a PHP command into a JS console message :) Normally this kind of answer is considered self promotion (even being open source and such) and really doesn't address the programming issue at hand...
Thanks for the clarification @brasofilo :) I guessed that the downvotes were because of that. I'll do as you suggest and I'll try to explain the technique I used. Even if coding it every time from scratch is not very practical (hence the need for a plugin), someone could find it interesting.
Cool :) Oh, and there's no problem in answering old questions. Years from now, they will probably keep being visited and provide help to some lost programming souls, lol
@fedeandri Thanks for the great plugin. It's really useful and saved my day. Really glad I found your plugin. Keep the good work up. You should deserve more upvotes.
@brasofilo after "only" 4 years, I finally remembered to follow your recommendation (would be appropriate a [smiling with a cold sweat] emoji here)
|
-1

By using Visual Studio and a dev tools php debugger extension, you can actually debug it step by step rather than logging them and trying to figure out the variable values during the run time. There might be other alternatives as well.

PS: It is not free :(

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.