Run WP-CLI with Xdebug using Herd Pro

I’ve been using Herd Pro for local development recently, and it works great. One really cool feature is the automatic Xdebug detection, which turns on Xdebug automatically if required (either through a detected breakpoint in PHPStorm or using a browser extension).

I often work with custom WP-CLI commands and sometimes want to enable Xdebug when running these commands. To do so, you can run the following command:

Terminal
herd debug $(which wp)

herd debug .. is a replacement for php .. but with Xdebug enabled, and $(which wp) points to the path of the WP-CLI executable.

If you want to run wp post list but with Xdebug enabled, you can run herd debug $(which wp) post list instead.

This gets tedious quickly, so I also recommend adding this to your ~/.bashrc or ~/.zshrc file:

Terminal
alias wpx='herd debug $(which wp)'

Now, you can use wp to run WP-CLI without Xdebug, and wpx to run WP-CLI with Xdebug enabled.

Written by Daniel Post

Hi! I’m Daniel Post, a freelance full-stack WordPress developer from the Netherlands. This is my personal website, where I share articles and guides related to WordPress.

I am also available for hire, so if you’re looking for a developer for your next project feel free to get in touch!


4 responses to “Run WP-CLI with Xdebug using Herd Pro”

  1. Shahid Avatar
    Shahid

    I have a question regarding custom post type?
    Can the slug be removed from the custom post type?
    E.g. http://localhost/author/daniel –> http://localhost/daniel

    1. Daniel Post Avatar

      I believe it’s technically possible with some custom code to 1) change the permalinks to remove the slug, and 2) add custom rewrites, but I think it might cause other issues so I’d advise against it. Why do you want to remove the slug?

      1. Shahid Avatar
        Shahid

        I just wanted to know. This can be one of the use case.
        https://core.trac.wordpress.org/ticket/34136#comment:12

  2. Marinus Klasen Avatar

    Thanks for sharing, very useful for Herd setups 🙂

    I’ve customised it a bit so I can keep using the ‘wp’ command:

    “`
    curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
    chmod +x wp-cli.phar
    sudo mv wp-cli.phar /usr/local/bin/wp-cli
    grep -q “alias wp=’herd debug /usr/local/bin/wp-cli’” ~/.zshrc || echo -e “\n# wp-cli alias\nalias wp=’herd debug /usr/local/bin/wp-cli’” >> ~/.zshrc
    “`

Leave a Reply

Your email address will not be published. Required fields are marked *