5 Ways to Work Faster Using the WordPress Command-Line

5 Ways to Work Faster Using the WordPress Command-Line


Anything speeding up or taking the tedium out of routine development and management tasks is goodness. If you're creating and managing WordPress sites here are five ways the WordPress command line interface (WP-CLI) can make your work faster and easier.

1. Database Backups

Backups are incredibly important and incredibly tedious. The simpler they are to do, the more likely they are to happen. This one liner creates a database backup:

  $ wp db export backup-filename.sql
  $ wp db export - | gzip > backup-filename.sql.gz

Likewise, loading a backup is equally as simple:

  $ wp db import backup-filename.sql
  $ gunzip -c backup-filename.sql.gz | wp db import -

2. Search & Replace

Maintaining a development version of your website for testing is essential. Anyone who's ever tried to duplicate a WordPress site knows the biggest challenge (and pain point) is changing the site URL. The search and replace utility makes this simple:

  $ wp search-replace dev.mysite.example

If your source site is https and your development site is http, you'll need to do it in two steps:

  $ wp search-replace http://dev.mysite.example
  $ wp search-replace dev.mysite.example

Note, some plugins also store the site URL in temporary files. For example, Elementor. In these cases you'll also need to regenerate the files through the plugin.

3. Installing WP from the command line

Installing WordPress is straightforward, but it's also fiddly. Fortunately it can be done in 3 commands, and without touching a web browser. Have your database name, username, and password ready before you start.

  $ wp core download
  $ wp core config --dbname=dbname --dbuser=username --dbpass=password
  $ wp core install --title="My New Site" --admin_name=siteadmin --admin_password=longandsecret

If you're worried about passwords in your command line history buffer, you can clear them after with:

  $ history -c

4. Deleting Content

Creating a new site often involves importing content from the old site. Many, many times. This can leave the new site with a lot of crufty posts, and deleting posts from WordPress is tedious. Fortunately, there's an easy way to clean the slate between imports.

  $ wp post delete $(wp post list --post_type='post' --format=ids)
  $ wp post delete $(wp post list --post_type='media' --format=ids)

Use with care. And remember item 1, it's easy to make a backup beforehand!

5. Running PHP

Sometimes it's nice to see the output of a WordPress function without writing an entire webpage. The eval function lets you run PHP from the command line. For example, the following one-liner prints the login URL:

  $ wp eval 'print wp_login_url() . "\n";'

This is also useful if someone has changes the login URL with the iThemes Security plugin and doesn't tell you the new URL!

This one-liner shows you the output of the WordPress get_terms() function:

  $ wp eval "print print_r(get_terms(), 1);"

You can also run full PHP scripts with the eval-file sub-command, but that's another article.