By getting started, I mean really just getting started. I’m just going to show you how to get WP-CLI running. I think once you do, you’ll take off on your own.
If your local development environment is on VVV, then stop reading, you’ve already got WP-CLI installed!
Here’s a summary of what we’re going to do:
- Download the application
- Make it executable from the command line
- Move and rename it
- In case of XAMPP (or MAMP), add its binary directory to PATH (need to make sure XAMPP’s php is used, and not your machine’s)
Yes, the instructions for installing WP-CLI can be found on its site, but sometimes I just like to reiterate and expand.
WP-CLI is a command line tool, so presumably you already have some familiarity with Terminal, or your command line application of choice. Go ahead and open that up.
I’m going to install this on my local machine, but you could just as easily do this on a web server.
First, we need to download the application. WP-CLI is available as phar file, that is, a php archive – all of the files needed bundled into one easy to manage file. You may also clone the git repo if you wish, but I’m not covering that here.
Go to the releases page and grab the link to the latest phar file. Or,
https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar should always be the latest stable package.
You can use
wget to download it (sure, you could just click the button, but where’s the command line love?).
curl -L https://github.com/the-url > wp-cli.phar
wget -O wp-cli.phar https://github.com/the-url
With curl, you might get an “SSL certificate problem” warning, in which case just add the -k flag.
curl -L -k https://github.com/the-url > wp-cli.phar
I have intentionally not included the valid URLs. You should go grab the url for the most recent version instead of whatever was current when I wrote this, especially in case the files have moved.
To test, we can run a simple command (from the same directory where we’ve just downloaded wp-cli.phar),
php wp-cli.phar --info. You should get output like this
PHP binary: /usr/bin/php PHP version: 5.4.17 php.ini used: WP-CLI root dir: phar://wp-cli.phar WP-CLI global config: WP-CLI project config: WP-CLI version: 0.14.0
If you get nothing, the phar was not downloaded correctly – try that again.
Of course, we don’t want to constantly preface the command with ‘php’ so let’s make it executable.
chmod +x wp-cli.phar
Now if you’d like, you can try
Move and rename
Still could be better. Now we’ll rename it to a nice short ‘wp’ and move it so it’s available from anywhere, not just the current directory.
mv wp-cli.phar /usr/bin/wp
At this point, you may be told permission denied. Just run
sudo !! to rerun the last command as sudo, and enter your password.
Time for more testing. Without changing directories, run
wp --info. You should be getting that same info we got earlier. Good job!
But now we have to to do the real test. In Terminal, navigate to the root of your local WordPress install. In my case, I’m running XAMPP, and WordPress is installed in a subdirectory called stable, so
Now let’s see if we can get a list of the available WP-CLI commands. Run
wp help. If you get a list, kudos, you can skip the next little section. But if you’re like me, you might be getting some HTML indicating a database connection error. Fear not, we shall conquer this.
Basically what’s happening is the wrong php is being used when running the command. We need to add the binaries provided by *AMP to PATH. To do this, we’re going to edit the
.bash_profile file located in your home directory.
You could use
nano ~/.bash_profile or
vi ~/.bash_profile, but I like Sublime Text, so I use
open ~/.bash_profile -a "Sublime Text" (actually, I have a shortcut for that in my .bash_profile:
alias st='open -a "Sublime Text.app"' so I can just use
something something command line love shut up
So what we need to do is check if *AMP’s bin path is already in PATH, and if not, prepend it. If the *AMP path is not listed first, the wrong php will continue to be used.
## # WP-CLI / XAMPP compat ## echo $PATH | grep -q -s "/Applications/XAMPP/xamppfiles/bin" if [ $? -eq 1 ] ; then export XAMPP_PATH=/Applications/XAMPP/xamppfiles/bin export PATH="$XAMPP_PATH:$PATH" fi
We do the if check otherwise that the XAMPP path is prepended every time you source the bash file. Speaking of, we need to do that:
Check that this worked by echoing PATH in the Terminal
echo $PATH. Hopefully you now have something like
Note: If not using XAMPP, or possibly a different version of XAMPP, your path may be different, and you may need to poke around a bit. For example, MAMP’s may look like this
Okay, okay, okay
At this point, we’ve downloaded, made it executable, moved and renamed it, and possible even modified our PATH.
In your WordPress install’s root, you should be able to run
wp help and get a list of available commands.
To get help on a specific command, use
wp help [command], for example,
wp help post, and then even
wp help post create.
Enjoy! Have fun!
If you need a birds-eye view of what’s going on, here’s a
* In the screencast, I don’t add the *AMP path the way I should. Fortunately, I think it’s unlikely anyone will copy that when there’s a nice copy-pasta snippet above. 🙂^