Debugging WordPress PHP Code with Xdebug and VS Code

Debug WordPress with Xdebug and VS Code

var_dump() and error_log() show one value at one moment. A step debugger stops the code on a line you choose and shows all variables, the call stack and the current hook, and lets you go on line by line. For WordPress, with its chains of hooks and filters, this saves hours.

Problem: Finding out why a hook or a query does not behave as expected takes dozens of var_dump() calls.

Solution: Install the Xdebug extension, enable remote debugging in php.ini, install the PHP Debug extension in VS Code, and start listening for Xdebug. The page request then stops on your breakpoints.

Installing Xdebug

On Ubuntu or Debian:

sudo apt install php-xdebug
# or, if there is no package for your PHP version
sudo pecl install xdebug

Check with php -v: the output should contain “with Xdebug”. Remember that the web server can use a different PHP than the command line; phpinfo() in the browser shows the one that matters.

Configuring Xdebug 2

In php.ini or a separate 20-xdebug.ini:

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_host=127.0.0.1
xdebug.remote_port=9000

remote_autostart starts a debug session on every request. Without it, a session starts only when the request has the XDEBUG_SESSION_START parameter or cookie, which the Xdebug helper extensions for the browser set with one click. Restart PHP-FPM or Apache after the change.

VS Code

Install the PHP Debug extension and add .vscode/launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/var/www/html/wp-content/themes/mytheme": "${workspaceFolder}"
            }
        }
    ]
}

pathMappings is needed when the code runs in a virtual machine or a container: the left side is the path on the server, the right side the folder open in VS Code. If PHP runs on the same machine, leave it out.

Press F5 (“Listen for Xdebug”), set a breakpoint by clicking left of a line number, and reload the page.

WordPress debug constants

In wp-config.php of the local site:

<?php
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );      // writes to wp-content/debug.log
define( 'WP_DEBUG_DISPLAY', false ); // do not print errors into the page
define( 'SAVEQUERIES', true );       // keep all queries in $wpdb->queries

With a breakpoint after a query, $wpdb->queries in the variables panel shows every SQL query of the request with its time and the function that ran it.

Update (2020): Xdebug 3 renamed the settings: xdebug.mode=debug, xdebug.start_with_request=yes, xdebug.client_host and xdebug.client_port, and the default port changed from 9000 to 9003. Change the port in launch.json as well.

NOTE: Never enable Xdebug on a production server. Even without an active session it slows down every request, and an open debug port is a security problem.