0

In my WordPress plugin, there is a PHP file called app_setup.php. I want to debug it in Visual Studio Code.

So I do as follows:

  1. Install WAMP Server on my Windows Server.
  2. Create a virtual host at 127.0.0.2 and install a WordPress site on http://127.0.0.2
  3. The plugin will be invoked via a shortcode. So I create a new post in WordPress and add a shortcode in it. The URL of the post is http://127.0.0.2/test/
  4. In VS Code, I follow the instructions to install PHP Debug Adapter for Visual Studio Code.
  5. Then in VS Code, under the folder of the plugin, I choose "Add configuration" and create 3 configurations.
  6. I open app_setup.php and set a breakpoint in it.
  7. Then I choose "Launch Built-in web server" configuration and click the arrow to the start the debug.
  8. VS Code will launch Chrome and visit a URL http://localhost:50334/, where 50334 is a random port. Since there is no page at http://localhost:50334/, it shows a 404 error.
  9. I try to create a new tab in Chrome and visit my post http://127.0.0.2/test/ and execute the plugin code, but the breakpoint in VS Code will never be triggered.
  10. If I start debugging app_setup.php in VS Code use the configuration "Launch currently open script", then the breakpoint will be triggered in VS, but all WordPress functions are not recognized since it is not executed in a WordPress environment, it will report error like below:
Fatal error: Uncaught Error: Call to undefined function plugin_dir_path() in C:\wamp64\www\blogs\wp-content\plugins\myplugin\app_setup.php on line 10

So, what is the proper way to debug a PHP file in WordPress plugin?

8
  • " I choose "Add configuration" and create 3 configurations." What their names? Use the one that says "Listen" ("Listen for Xdebug connection" or something along that line). Then install Xdebug helper extension in your browser. What it does -- it sets the Xdebug cookie (that acts as "debug me" flag). When you use that config in VSCode, then navigate to the page you want to debug, activate that Xdebug extension and refresh the page (or submit the form/click the link).
    – LazyOne
    Commented Feb 15 at 18:17
  • 1
    Alternatively you can configure Xdebug to try and debug every single request (not desired, but may be easier if you are new to Xdebug / hard to understand this all). xdebug.start_with_request = yes
    – LazyOne
    Commented Feb 15 at 18:19
  • You may also check the following page. It's for PhpStorm, but the overall approach is the same. jetbrains.com/help/phpstorm/zero-configuration-debugging.html (sadly, I do not have any similar links for VSCode)
    – LazyOne
    Commented Feb 15 at 18:19
  • for VsCode, see: PHP Debug Adapter for Visual Studio Code.
    – Luuk
    Commented Feb 15 at 18:28
  • 1
    @LazyOne, Thank you very much. I have use your suggestion xdebug.start_with_request = yes and it works.
    – alancc
    Commented Feb 16 at 17:18

0