admin管理员组

文章数量:1287593

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

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);
Share Improve this question edited Oct 19, 2018 at 14:01 asked Oct 19, 2018 at 12:50 user145078user145078 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
Add a comment  | 

1 Answer 1

Reset to default 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:

本文标签: theme developmentUse debugging for add action events