WP-CLI is a powerful tool that can help make common WordPress maintenance tasks easier, especially if you want to automate those tasks using cronjobs in a secure way. Here at Bluehost, we love this tool, and even provide it pre-installed on every hosting account.
There are some gotchas that can confuse even developers that are already familiar with WP-CLI. So I wanted to quickly outline them.
Using the Pre-Installed Command
The official instructions for WP-CLI instruct users to install it as the super short and convenient “wp” command. However, because our shared hosting accounts support multiple versions of PHP including version 5.2, which WP-CLI does not support, we were required to provide a wrapper around this command called “wpcli”. So every time you see instructions for running a WP-CLI command, you must replace “wp” with “wpcli”. This wrapper already knows how to force WP-CLI to always use PHP 5.4 (or later). For example, here’s how to check which themes you have installed:
email@example.com [~/public_html]# wpcli theme status 6 installed themes: I smallbiz 3.8.6 Lite I twentyeleven 1.7 A twentyfourteen 1.0 I twentyten 1.6 I twentythirteen 1.1 I twentytwelve 1.3 Legend: I = Inactive, A = Active
We rely on this tool ourselves to help provide you higher quality support, but in the interest of security, we’re required to review updates to any tools we install on our servers, so sometimes our version of WP-CLI might not be the latest version available. If you find yourself needing some newer functionality or fixes to bugs within WP-CLI, you might still want to install the latest version on your hosting account yourself…
Installing WP-CLI on your Hosting Account
Since shared hosting does not provide you with root access to the server, the recommended installation instructions provided by WP-CLI won’t work. Instead, we’re going to install WP-CLI as a “user-specific installation”. It is still perfectly possible to install WP-CLI without root access without losing any functionality provided by WP-CLI.
We are still going to download WP-CLI in the recommended way, however, we are going to download it into your ~/bin directory instead:
firstname.lastname@example.org [~]# cd bin/ email@example.com [~/bin]# curl -L https://raw.github.com/wp-cli/builds/gh-pages/phar/wp-cli.phar > wp-cli.phar % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1075k 100 1075k 0 0 1731k 0 --:--:-- --:--:-- --:--:-- 1798k
And rather than using the “php” command as instructed by WP-CLI, Bluehost accounts are required to explicitly use the “php-cli” command to run PHP in command line mode. So you can check that WP-CLI is working correctly by running this:
firstname.lastname@example.org [~/bin]# php-cli wp-cli.phar --info PHP binary: /ramdisk/php/54/bin/php54-cli PHP version: 5.4.24 php.ini used: /usr/php/54/etc/php.ini WP-CLI root dir: phar://wp-cli.phar WP-CLI global config: WP-CLI project config: WP-CLI version: 0.14.0
WP-CLI is actually installed now, however, it’s not very convenient to use still. You don’t want to have to run “php-cli ~/bin/wp-cli.phar” every time you want to run a WP-CLI command, so we’re going to setup an alias for it so you can just run “wp” like you normally would.
To do this, we need to edit our ~/.bashrc file, and add our alias right alongside any other aliases setup for our account. You’ll notice that Bluehost has provided a few basic ones in this file already. We’re just going to add one more, the location in the file isn’t important (but I like to organize all of my aliases in one spot next to each other). Just add the following line to this file:
alias wp='php-cli ~/bin/wp-cli.phar'
Once you save this file, we’re done. You can either log out and SSH back in for this alias to be loaded, or you can run the following command to get it working immediately:
email@example.com [~]# source .bashrc
Now we’re in business:
firstname.lastname@example.org [~/public_html]# wp plugin list +--------------------+----------+-----------+---------+ | name | status | update | version | +--------------------+----------+-----------+---------+ | akismet | inactive | none | 2.5.9 | | better-file-editor | active | none | 2.1.2 | | hello | inactive | none | 1.6 | | jetpack | active | available | 2.8 | +--------------------+----------+-----------+---------+