Install and set up wp-cli WordPress on shared and local hosting

WordPress has a command line tool called wp-cli which operates similar to what Drush does for Drupal, it runs on OSX/Linux operating systems with a minimum of PHP 5.3.2 or later and WordPress 3.5.2.

This guide will go through getting and installing wp-cli and basic usage it covers for either a local or production server environment.

Install wp-cli

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

Make it executable

chmod +x wp-cli.phar

Move it into your Path or Alias it

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

In the above command as well as moving the file name is also changed to just wp

or alias from your home into .bashrc (good if you are sharing the webserver)

echo "alias wp='~/wp-cli.phar'" >> .bashrc

reload

source .bashrc

Test

Test it by running

wp --info

Output should be similar to

PHP binary:/usr/local/php5-5.5.5-20131020-222726/bin/php

PHP version:5.5.5

php.ini used:/usr/local/php5/lib/php.ini

WP-CLI root dir:phar://wp-cli.phar

WP-CLI global config:

WP-CLI project config:

WP-CLI version:0.16.0

Now all you have to do is operate the wp-cli commands by changing into the WordPress webroot folders, for example to get a list of plugins for a WordPress install change into the webroot directory and run:

wp plugin list

Database Errors

“Error establishing a database connection” maybe be displayed when running the command – this would be because the PHP binary being used is not the default one set by the operating system. wp-cli will first look for the PHP binary in /usr/bin/ if you have used an AMP solution like MAMP or upgraded your PHP you need to declare that PHP binary location earlier in your path

Once all is working explore the available commands by running wp to see the list of the commands and then more specifically by running wp help and the command name such as:

wp help plugin

Installing and Configuring a WordPress Using wp-cli

You have to have a database already set up and have dbname dbuser and password.

Change directory into your webroot.

Download WordPress

wp core download

Create a Database wp-config.php file

wp core config --dbname="DBNAME" --dbuser="DBUSER" --dbpass="DBPASSWORD"

Insert your values and drop the quotes

Finish the Install

wp core install --url="http://BLOG_URL"  --title="BLOG_NAME" --admin_user="ADMIN_USER" --admin_password="PASSWORD" --admin_email="ADMIN_EMAIL"

Insert your values and drop the quotes

After which you should see:

Success: WordPress installed successfully.

 Using the wp-cli Completion Script

It is also a good idea to use the wp-cli bash completion script so you can tab complete commands.

Download and file it in your home folder and reference it in your .bash_profile

source ~/wp-completion.bash

Just type wp and tab to see available commands.

Further reference.

1 Comments

  1. graham on August 9, 2016 at 10:35 pm

    Your comment is awaiting moderation.

    I followed the install instructions at wp-cli.org and am unable to connect to database. I am using a newly installed (this morning) version of MAMP PRO.

    which php
    /usr/bin/php

    echo $WP_CLI_PHP
    /Applications/MAMP/bin/php/php5.5.14/bin/php

    echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/share/dotnet:/Applications/MAMP/bin/php/php5.5.14/bin/php

    wp –info
    PHP binary:/usr/bin/php
    PHP version:5.5.14
    php.ini used:
    WP-CLI root dir:phar://wp-cli.phar
    WP-CLI packages dir:
    WP-CLI global config:
    WP-CLI project config:
    WP-CLI version: 0.24.

    Then, I followed a few other tutorials/instructions:

    sudo -s
    curl http://wp-cli.org/packages/phar/wp-cli.phar > /usr/share/wp-cli.phar
    echo ‘/Applications/MAMP/bin/php/php5.3.14/bin/php /usr/share/wp-cli.phar $@’ > /usr/bin/wp
    chmod +x /usr/bin/wp

    and I am still getting Error: Error establishing a database connection

    Any ideas?

Leave all Comment