Fifteen Logo
blog

WordPress CLI (WP-CLI) – A Command Line Tool to Save the Day

April 15, 2019

The Command Line

If I can take you back 10 years ago, I was building websites and the command line was nothing more than a scary prospect. I’d reluctantly open PuTTY to ssh into a server to fix some sort of permission error or; at a push, restart Apache. There was no such thing as a build tool, I wasn’t utilising an operating system that could be described as a Unix variation and there just wasn’t any sort of need to jump into the command line.

Today, things are a little different. My day to day machine is a Macbook Pro running OSX, almost every project I touch uses gulp, grunt, bower or webpack and I’m now responsible for (at the time of writing this) 75 servers. There isn’t really a moment of my working day where terminal is not open. From simple copy/paste commands through to configuring a horizontally scaled cluster on the other side of the world, the command line is my best friend nowadays.

Command Line Example

WordPress Command Line Interface

This leads me on to the subject matter I’d like to write about today. WP CLI (WordPress CLI). WP CLI is a command line interface that enabled interaction with WordPress, it’s functionality, it’s data and its dependencies via the terminal. It labels itself as an alternative to the WordPress Admin and whilst it hasn’t (and won’t) completely eradicated the need for the GUI it’s helped me out of a couple of tricky situations and it certainly allows you to complete repetitive tasks a lot quicker. If you’ve ever installed a plugin or theme that has completely broken your website and you’re unable to gain access to WordPress Admin this is definitely the tool you’re looking for.

What can I do with WP CLI?

I’m not going to mirror the WP CLI Handbook but some of my common use cases include:

  • Downloading WordPress Core – this will download and unpack WordPress into the current working directory
  • Adding / Updating / List Users – really useful for those forgotten admin passwords
  • Installing, activating and deactivating Plugins – great for those plugins that have broken access to WordPress Admin
  • Clearing Caches – handy if you like to periodically clear the cache via a CRON job
  • Generating config files – combining this with WP CLIs core download functionality make for rapid deployment of WordPress
  • Search/replace strings in the database – useful when changing website addresses
  • List rewrite rules – super helpful when debugging problems

Command Prompt Example

How to install WP CLI

It’s really easy. The only thing you need to ensure you have is an account in the sudoers (admin) group on the relevant server. Installation of WP CLI is accomplished with 4 simple commands.

$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

$ php wp-cli.phar --info

$ chmod +x wp-cli.phar

$ sudo mv wp-cli.phar /usr/local/bin/wp

This has installed WP CLI system-wide. All you need to now is navigate to a directory that contains a WordPress install and issue the following command to confirm successful installation.

$ wp --info

Commonly Used WordPress CLI Commands

I’ve compiled a list of my favourite and most use WP CLI commands below. I’d urge you to head over to the WP CLI Handbook to get a full understanding of all of the different types of commands available.

Download WordPress Core

$ wp core download

Install WordPress (with –parameters)

$ wp core install --url=example.com --title=Example --admin_user=supervisor --admin_password=strongpassword [email protected]

Get current WordPress version

$ wp core version

List all WordPress users

$ wp user list

Create users (with –parameters)

$ wp user create bob [email protected] --role=author

Update a user (with –parameters)

$ wp user update 123 --display_name=Mary --user_pass=marypass

Flush all W3 Total Cache caches

$ wp total-cache flush all

List all plugins

$ wp plugin list

Download, install and activate a plugin from wordpress.org (bbpress in this instance)

$ wp plugin install bbpress --activate

Delete a plugin

$ wp plugin delete hello

Activate a plugin

$ wp plugin activate hello

Deactivate a plugin

$ wp plugin deactivate hello

At Fifteen we find solutions to problems every day. Whether developing a new site, discovering the nuances of brand DNA or designing stunning artwork, we roll up our sleeves and take on every obstacle that comes our way. If you’re looking for a digital company that is here to provide answers and not questions contact us today.

Share

GET MORE

WANT REGULAR BLOG UPDATES TO YOUR INBOX?

Stay on top of your digital game with our blog updates.

Newsletter
More posts

OTHER BLOGS YOU MAY WANT TO READ...