10 advanced WP-CLI tricks

If you like my WordPress work, check out my new plugin, Bylines. Thanks!

These examples are adapted from my “WP-CLI: Advanced Usage” VIP Workshop presentation last week. I will now only ever use GIFs in slides.

wp-cli update --nightly

Live life on the bleeding edge by running latest and greatest version of WP-CLI.

The master branch is typically stable enough to use in local and staging environments. Or production too, yolo.

wp scaffold theme-tests

You probably knew you could scaffold plugin tests, but you probably didn’t know that you could also scaffold theme tests.

wp scaffold theme-tests gives you everything you need to write PHPUnit tests that leverage the WordPress test suite. Specifically:

  • bin/install-wp-tests.sh is a setup script to download the WordPress test suite and create the test database.
  • .travis.yml is a configuration file to automatically run your tests every time you push to GitHub (Travis account required).
  • tests/ contains the PHPUnit-specific files. You can modify test-sample.php to your liking.

Functionally, theme tests are pretty much the same as plugin tests. Check out the generated tests/bootstrap.php file for the secret of how your theme gets loaded into the test suite.

wp role reset

If you’ve been monkeying about with core’s default roles, wp role reset will set them back to the basics.

Reset a single role, or reset them all with --all.

wp rewrite list --match=<uri>

Rewrite rules are WordPress’ API for routing URI patterns to WP_Query instances.

For historical reasons, rewrite rules are stored in an option, which means you need to “flush” (regenerate) rewrite rules any time you register a new one.

The --match=<uri> argument makes it possible to see which rewrite rules match the URI you’ve provided, making it simple to debug why your rewrite rule doesn’t seem to be working. Because rewrite rules never work the first time.

wp shell

Ever wish you could literally write and run PHP code against WordPress?

Well, now you can. Actually, you’ve been able to for years.

wp site empty

If you want to delete all content in your WordPress install, but keep users, options, etc., wp site empty is your new best friend.

wp cron event run

After the Rewrite API, WP Cron is the WordPress developer’s second most bane of their existence. It’s opaque, buggy, and unreliable.

But, we have to use it, so we may as well make it easier to work with.

--prompt

There are some commands I still can never remember the arguments for.

--prompt works with any WP-CLI command, even those you write yourself, and interactively walks you through all of the arguments you need to complete.

wp-cli.yml

WP-CLI supports global- and project-based configuration files to accelerate your workflow.

For instance, if you use the same command with the same arguments over and over again, you can define those arguments as the default arguments for the command.

“Ugh, I always have to enter the same username and password when installing WordPress”… no you don’t!

Bonus!

You must be a WP-CLI master.

Here are four challenges for you to complete (with WP-CLI) in two minutes or less. For the solution, click on the link to see the GIF: