Is it possible to debug WP function using wp_cli in PHPStorm?

Hi all,

I've got a Wordpress/Woocommerce project and have installed WP_CLI. I have a static function called test in my App controller:

public static function test( $num ) {
  $order = wc_get_order( $num );
  $nums = [];
  foreach ($order->get_items() as $item) {
    $nums[] = $item->get_product_id();
  }
  return $nums;
}

I can call this from WP_CLI on the command line like this:

wp shell
wp> App::test(<order_id>)

This prints out an array of the product_ids to the console:

array(1) {
  [0] =>
  int(<product_id>)
  [1] =>
  int(<product_id>)
}

If I put a breakpoint into the function it does not work from the wp shell interface. Works fine in the normal way when listening for incoming connections from a browser.

Is this even possible? How can I configure so that Xdebug works from wp shell ?

Thanks in advance for any thoughts

Kevin

0

Hi!
 

That's a bit tricky scenario, yet it's possible.

 

In PhpStorm it's required to map both wp cli executable(and it must be available locally) and project dir, e.g.:

 

Afterwards, you'll have to pass variables manually into wp-cli command. For example:

```

XDEBUG_MODE=debug XDEBUG_SESSION=1 /opt/php/8.3/bin/php ../wp --allow-root shell

```

Which would default to 9003 localhost. If phpstorm is reachable, you'll get an xdebug window popup once breakpoints are set:

0

Hi Stefan, thanks for the detailed reply. I'm a bit stuck with setting up the mapping. I've run `wp --info` from within my project and this is what it's telling me about how wp-cli is set up:

And in my project root I've got a wp-cli.yml file who's contents are:

path: public_html/wp
server:
  docroot: public_html

Given the above, are you able to advise how I would set up the mapping?

Much appreciated.

0

Hi Kevin,

It is enough just to have `wp` in your local project mapped to the one on server(first screenshot, mappings). The `.yml` basically simplifies the `wp` command calls. 

 

> Given the above, are you able to advise how I would set up the mapping?

Therefore, in the mapping settings:

File/Directory = path to `wp` in your local project

Absolute path on the server = absolute path to `wp` on the server

And of course, paths to WP document root in both server + local project, as I have shown earlier as well.

 

However, for testing purposes I would recommend running(on the server, of course, not in local env) following the command I've given earlier. 

You may also want to use the breakpoint and command I've shown earlier just to make sure xdebug is working. 

 

0

请先登录再写评论。