Search
Close this search box.

An Introduction to WP CLI

Table of Contents

Sponsored Ad

If you purchase through a link on our site, we may earn a commission.

WordPress is a nice platform, but setting up a fresh install or doing maintenance work can be a bit of a hassle. Especially when you need a fresh install every odd week or-so (like me), or you have a lot of WordPress sites to maintain (again; like me). That’s why I love WP CLI.

WP CLI is a command line interface for WordPress. It allows you to install, setup, control and maintain WordPress through your command line. It was initially created by Andreas Creten and is now being maintained by Cristi BurcΓ’ (better known as Scribu). And is currently being developed very actively. Doing things through the command line might seem like a bit of a chore at first, but if you’re actively involved in WordPress plugin or theme development, you’re probably already used to using a command line tool for version control like GIT or SVN. Getting WordPress in on that action is just the next logical step. If you’ve never used the command line before, you might want to check out some other sources before continuing with this tutorial. This tutorial assumes you are at least familiar with SSH. If you’ve got that down, we’ll start with WP CLI! It’ll make WordPress development easier and more fun, I promise… πŸ˜‰

Installing WP CLI

I use WP CLI on my local machine and on my staging server. We’re going to be installing WP CLI right through the command-line. On your local machine you’ll of course have instant command line access and on a staging server you can use SSH to get command line access. Once you’ve logged in successfully, most servers will be able to install WP CLI for you with just one line:


curl  | bash

If you’re using MAMP there’s a bit more to it… check out these alternate install methods.

If everything went well, you are now able to use WP CLI! Congratulations! πŸ™‚

Your First Commands

Okay, I don’t want to give away all the awesome immediately but I just can’t help myself;
the biggest perk to working with WP CLI is update automation. Updating WordPress core is as easy as running

 $ wp core update 

Now, you might say ‘well, that’s nice… but having to remember and type that stuff is a lot harder than just clicking that big blue button’. Well, you’ve got a point, but what if you’d have to click that blue button a bunch of times? What if you needed to update plugins?

 $ wp plugin update --all 

bash

Yep. that’s right. With one simple command, the plugins in your WordPress install can be updated. Of course we have to be smart around updates; there’s always something that can go wrong. That’s why it’s always a good idea to backup the database before updating… fortunately this becomes super-easy with WP CLI:

 $ wp db export 

Now, when we combine these commands in one simple bash-workflow, we get to safely update every part of a WordPress installation. And if we tie that bash-script to a simple alias, we get to run this stuff with just one simple command.


#setting the alias in .bash_profile
alias updatewp='bash ~/path/to/bashfile'

#bashfile: 
wp db export
wp core update
wp plugin update --all
wp theme update --all

Installing your Favorite Plugins

I know WordPress.org has an option to mark your favorite plugins, but i’d much rather automate that stuff entirely when I start a new project. So I made a little bash file of the plugins I use on every project;


wp plugin install wordpress-seo
wp plugin install w3-total-cache
wp plugin install limit-access
wp plugin install wp-migrate-db
wp plugin activate wordpress-seo
wp plugin activate w3-total-cache
wp plugin activate limit-access
wp plugin activate wp-migrate-db

Running this script on a fresh install, will download, install and activate four plugins for me. And if I get greedy I can add as many plugins to this bash-file as I want. I told you this was going to make your life easier πŸ˜‰

Creating New Installs

WP CLI can even create new and fresh installs for you. It just needs some valid database information. This can be very handy on your local machine or a staging server where you’ll need fresh installs often. The only thing I didn’t like about this solution was me having to create a database every time I needed a fresh install. So I usually work with one big dev-database where multiple WordPress versions coexist. I can do this by changing the prefix of each WordPress install. Next to this being very easy for development, changing the prefix is great for security reasons.



#First, name and create a subfolder:
printf "In which folder should we install WordPress? "
read NEWDIR
mkdir $NEWDIR

#Install WordPress and create the wp-config.php file...
#We add the --force so WP CLI doesn't get confused by some other wp version that might be running on this domain.

cd $NEWDIR
wp core download --force
wp core config --dbname={YOUR-DB-NAME} --dbuser={YOUR-DB-USER} --dbpass={YOUR-DB-PASS} --dbprefix=$NEWDIR"_"

#Set some variables:
wpurl={ YOUR-MAIN-DEV-URL }/$NEWDIR

printf "What admin username would you like to use?"
read WPADMINNAME

printf "What's your email?"
read WPADMINEMAIL

printf "Which password would you like?"
read WPADMINPASS

printf "What is the title of this WordPress site?"
read WPNAME

#finish the last step of a usual WordPress installation
wp core install --url=$wpurl --title=$WPNAME --admin_name=$WPADMINNAME --admin_email=$WPADMINEMAIL --admin_password=$WPADMINPASS

Now imagine this script, and combine it with the auto-installing favorites script and with one command, you’ll be good to go. If you have any more great commands, ideas or workflows, please leave a comment! I’d love to hear what you’ve come up with!

If you enjoyed this post, make sure to subscribe to WP Mayor’s RSS feed.

Luc Princen is a developer and designer from The Netherlands. He’s co-founder and technical lead at Chef du Web where he specializes in pushing WordPress to the limit.

Sponsored Ad

If you purchase through a link on our site, we may earn a commission.

All suggestions are anonymous.

More from our blog...

6 Responses

  1. Managing multiple WordPress websites is a hectic job, as you have to logging to each site and setup/install plugins and settings for each site independently.

    Luckily for those well versed to work in command line, they can use WP-CLI to manage multiple WordPress sites with just a few commands. WP-CLI comes with built-in commands specific just to manage WordPress websites.

    Through WP-CLI you can install, manage plugins and themes. Control settings such as cron-job, comments etc.

    Learn a few basic commands here:

  2. Ahh I am so glad I stumbled across this. I spend way too much time installing WordPress. Your guide is going in my bookmarks my friend. Thanks a bunch.

  3. Thanks for this introducing.
    I fixed some typos (? – admin_name has to be admin_user) and adjusted it a bit to my own needs. If anyone is interested ->

  4. If stuff regularly goes wrong when you update you are using badly written plugins and themes; WordPress is very VERY conscious about backwards compatibility.

  5. You can downgrade if you want. I run always trunk on my sites because I trust that. The same goes for the plugins/themes I use. So if you don’t trust something when updating then maybe you shouldn’t use that in the first place.

  6. WP CLI looks like it might be interesting for a number of reasons (and can do a lot more than is listed here – see , but it’s not something I’d use for managing updates to core (or plugins) right now. Having the ability to update easily from the command line is great, but if something goes wrong where is the rollback functionality? This is where GIT, SVN, Mercurial etc. come in to their own, especially if you’re managing a large number of WordPress sites.

Post a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Stay updated with WP Mayor's newsletter showcase every week

Stay on top of every new WordPress innovation and latest launches. Receive all our fresh product reviews and expert guides directly in your inbox.

Hosting Survey 2024

Are you happy with your hosting provider or are you over-paying for too little? Have your say below!

"*" indicates required fields

What's the main reason you picked this host?*
How happy are you with your host?*

OPTIONAL: If you'd like to receive the results of this survey, please enter your details below.