If you want to use debugging from an action where you won’t see the output, you can send things to error_log
. First, you need to enable debug logging. In your /wp-config.php
add the lines:
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
This will create a file called /wp-content/debug.log
Next, in your code, add some calls to error_log
and var_export
:
add_action( 'init', 'some_function' );
function some_function( $var ) {
error_log( var_export( $var, 1 ) );
}
Now, when init
runs our function, the contents of $var
will be dumped to /wp-content/debug.log
. Note the second parameter to var_export
says to return the output instead of trying to send it to be displayed.
Edit: 2021/09/17
Lately I’ve been using Ray for debugging things in general, and it’s particularly helpful when you can’t see the output of the script you’re running readily.
Installation
- Download the Ray app
- Install the package (they have a WordPress-specific plugin, I tend to just use the package directly)
composer require spatie/ray --dev
Usage
- Open the Ray app
- In your PHP, call
ray()
, e.g.
ray($var);
The Ray window will populate with the variable’s information: