2

Is it possible to vardump the variable inside the action function? i am not able to echo or see results when i am doing so.

This is not a woo commerce question but i am having problems with

add_action('woocommerce_order_status_changed','wc_order_uppdate_to_cancelled_status', 20, 4);
2
  • 1
    Where is the var_dump in this? To be honest I don't really understand your question. You can use var_dump() inside the function that is called from that action, yes
    – kero
    Commented Oct 19, 2018 at 13:24
  • @kero yes, that question was whether i can use var_dump() inside the function that is called from that action..i am changing the order status from the backend for above function but does not show the any var_dump values...may be it is effecting the users only..nothing in admin side ..thanks for the confirmation
    – user145078
    Commented Oct 19, 2018 at 13:28

1 Answer 1

5

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:

Ray window showing a Laravel exception

3
  • 1
    thank you exactly what i was looking for..if my question is confusing and if can be improved please feel free to do so.
    – user145078
    Commented Oct 19, 2018 at 14:00
  • hi, ` error_log( var_export( $var, 1 ) );` what is parameter 1 in this code?
    – user145078
    Commented Oct 19, 2018 at 14:10
  • 1
    See the second parameter to var_export on PHP.net. When the second parameter is truthy (e.g. 1, true, etc), var_export will return it's output instead of just dumping it to the screen.
    – phatskat
    Commented Oct 19, 2018 at 14:16

Your Answer

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