Getting Started with Xdebug
This guide shows you how to enable Xdebug in WordPress Playground and start debugging your code.
PHP WASM CLI vs Playground CLI
First, Xdebug is present in two different CLIs:
@php-wasm/cli: Run standalone PHP scripts. Use this when debugging PHP code without needing a WordPress environment.@wp-playground/cli: Run a full WordPress installation. Useful for debugging WordPress plugins, themes, or core functionality.
For this guide, we'll use @wp-playground/cli. If you're not familiar with the tool, we recommend reading the @wp-playground/cli guide, but the same process can also be applied to debugging PHP applications with @php-wasm/cli.
Quick start with npx
The fastest way to get started is using npx, which doesn't require installation:
npx @wp-playground/cli@latest server --xdebug
This starts WordPress on http://127.0.0.1:9400 with Xdebug enabled. Now you can connect a debugger.
Only one project can be debugged at a time.
Starting with IDE integration
Similar to the process with DevTools, let's use the same plugin code from before to debug with VS Code, and add the --experimental-unsafe-ide-integration=vscode flag. This flag will optimize the setup process for VS Code. If you're working with PhpStorm, add the --experimental-unsafe-ide-integration=phpstorm flag.
This flag is marked as unsafe because it edits the IDE config files to set Xdebug path mappings and web server details. CAUTION: If there are bugs, this feature may cause your IDE configuration files to break. Please consider backing up your IDE configs before using this feature.
To debug in VS Code, you'll need the following prerequisites:
- An extension to add PHP profiling support, for example, PHP Debug or another debugger extension to your preference.
- Have a
.vscode/folder. - Enable breakpoints in your IDE. Some IDEs come with this feature disabled, so be aware of this detail.
If everything is ready, you run the command:
npx @wp-playground/cli@latest server --xdebug --experimental-unsafe-ide-integration=vscode --auto-mount
If you don't have a .vscode/launch.json file, the terminal will create a file similar to this:
{
"configurations": [
{
"name": "WP Playground CLI - Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/": "${workspaceFolder}/.playground-xdebug-root",
"/wordpress/wp-content/plugins/test-xdebug": "${workspaceFolder}/"
}
}
]
}
Now, you can go to your code, add the breakpoints, start the debugging session named by your IDE, and happy testing.
